0

I saw similar questions but unfortunately I didnt found answer for my problem.

I have a list:

list = ['a_abc', 'a_xyz', 'a_foo', 'b_abc', 'b_xyz', 'b_foo']

I want to split this list into 3 based on character after underscore _. Desired output would be:

list_1 = ['a_abc', 'b_abc']
list_2 = ['a_xyz', 'b_xyz']
list_3 = ['a_foo', 'b_foo']

I would like to avoid something like:

for element in list:
   if 'abc' in element... 
   if 'xyz' in element...

because I have over 200 strings to group in this way in my use case. So code "should recognize" the same part of the string (after underscore) and group this in sublists.

Since I didnt notice similar issue any advice is highly appreciated.

1 Answer 1

5

You shouldn't want to do this with one or more lists, because you don't know at runtime how many there are (or, even if you know, it will be repeated code).

Instead, you can use defaultdict; it's like a default dictionary, but handles missing value simply creating a new element with your specified factory.

In this case, defaultdict(list) means to create a dictionary with a list factory; when a key is missing, the object will create an empty list for that key.

from collections import defaultdict

l = ['a_abc', 'a_xyz', 'a_foo', 'b_abc', 'b_xyz', 'b_foo']
d = defaultdict(list)
for el in l:
    key = el.split("_")[1]
    # key = el[2:]  # use this if the format of elements is <letter>_<other_chars>
    d[key].append(el)
    
print(d)
# defaultdict(<class 'list'>, {'abc': ['a_abc', 'b_abc'], 'xyz': ['a_xyz', 'b_xyz'], 'foo': ['a_foo', 'b_foo']})
print(d["abc"])
# ['a_abc', 'b_abc']
Sign up to request clarification or add additional context in comments.

2 Comments

defaultdict is indeed the cleanest way, however, if all of them start by <1 letter>_, you could just slice the string and remove the first 2 chars.
You're right, this is faster; I'll update the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.