I have a Python list mylist
whose elements are a sublist containing a string of a letter and number. I was wondering how I could split mylist
by the character at the start of the string without using code with individual statements/cases for each character.
Say I want to split mylist into lists a, b, c
:
mylist = [['a1'],['a2'],['c1'],['b1']]
a = [['a1'],['a2']]
b = [['b1']]
c = [['c1']]
It is important that I keep them as a list-of-lists (even though it's only a single element in each sublist).
This will work:
import itertools as it
mylist = [['a1'],['a2'],['c1'],['b1']]
keyfunc = lambda x: x[0][0]
mylist = sorted(mylist, key=keyfunc)
a, b, c = [list(g) for k, g in it.groupby(mylist, keyfunc)]
The line where sorted()
is used is necessary only if the elements in mylist
are not already sorted by the character at the start of the string.
EDIT :
As pointed out in the comments, a more general solution (one that does not restrict the number of variables to just three) would be using dictionary comprehensions (available in Python 2.7+) like this:
result_dict = {k: list(g) for k, g in it.groupby(mylist, keyfunc)}
Now the answer is keyed in the dictionary by the first character:
result_dict['a']
> [['a1'],['a2']]
result_dict['b']
> [['b1']]
result_dict['c']
> [['c1']]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With