0

I have a below list.

['A_1', 'A_2', 'A_3', 'A_4', 'B_1','B_2', 'C_1', 'C_2', 'C_3']

I need the output in below format.

[['A_1', 'A_2', 'A_3', 'A_4'], ['B_1', 'B_2'], ['C_1', 'C_2', 'C_3']]

I am very new to Python, is there any way to achieve the above output that it would be really helpful.

3
  • 2
    Does it mean that you want to group by the first letter? Commented Feb 25, 2022 at 7:02
  • 1
    What if it goes like ['A_1', 'B_1', 'A_2']? Commented Feb 25, 2022 at 7:07
  • 1
    @SharimIqbal you shouldn't have edited the code like this without confirming from the OP. It could be that they meant variable names. Commented Feb 25, 2022 at 7:09

4 Answers 4

1

This can be achieved directly with a dictionary combined with a loop:

  1. store each type of data in the form of "letter_", e.g. {"A_": ["A_1", "A_2"]}
  2. Loop through each data in the list, if the type is not in the dictionary, create a list with the prefix type of this data, otherwise add it directly to the list of this type
  3. Finally, directly convert the values of the dictionary into a list to achieve the requirement
category = {}
lst = ['A_1', 'A_2', 'A_3', 'A_4', 'B_1','B_2', 'C_1', 'C_2', 'C_3']
for item in lst:
    prefix = item.split("_")[0] + "_"    # Example of prefix values: A_, B_,...

    if prefix not in category:
        category[prefix] = [item]    
    else:
        category[prefix].append(item)

print(list(category.values()))
# [['A_1', 'A_2', 'A_3', 'A_4'], ['B_1', 'B_2'], ['C_1', 'C_2', 'C_3']]

Sign up to request clarification or add additional context in comments.

1 Comment

Using in on a dictionnary search in the keys ? Or is it because theres no str value that contains the prefix ?
1

I provided a easily understandable way (but requires more lines than other advanced thecniques)

You can use dictionary, in particular, I used defaultdict here to set default value as a list ([]).

I grouped the list by first letter in a for loop, as follows:

from collections import defaultdict

l = ['A_1', 'A_2', 'A_3', 'A_4', 'B_1', 'B_2', 'C_1', 'C_2', 'C_3']

group = defaultdict(list)

for item in l:
    first_letter = item[0] # saving items by first letter
    group[first_letter].append(item)

result = list(group.values())
print(result)
# [['A_1', 'A_2', 'A_3', 'A_4'], ['B_1', 'B_2'], ['C_1', 'C_2', 'C_3']]

Comments

1

You can use itertools.groupby for that. This assumes that the input is sorted, as in your example.

from itertools import groupby

lst = ['A_1', 'A_2', 'A_3', 'A_4', 'B_1', 'B_2', 'C_1', 'C_2', 'C_3']
output = [list(g) for _, g in groupby(lst, key=lambda x: x[0])]
print(output) # [['A_1', 'A_2', 'A_3', 'A_4'], ['B_1', 'B_2'], ['C_1', 'C_2', 'C_3']]

If for some reason you don't want to use import but only use built-ins,

lst = ['A_1', 'A_2', 'A_3', 'A_4', 'B_1', 'B_2', 'C_1', 'C_2', 'C_3']

output = {}
for x in lst:
    if x[0] in output:
        output[x[0]].append(x)
    else:
        output[x[0]] = [x]
print(list(output.values()))

Note that with an input list of ['A_1', 'B_1', 'A_2'] the two approaches will result in different outputs.

Comments

0

Refer below code:

input = ['A_1', 'A_2', 'A_3', 'A_4', 'B_1', 'B_2', 'C_1', 'C_2', 'C_3']

Use below logic:

ls_a,ls_b,ls_c = [],[],[] # Declaring empty lists
for i in input:
if 'A' in i:
    ls_a.append(i)
if 'B' in i:
    ls_b.append(i)
if 'C' in i:
    ls_c.append(i)
output = [ls_a,ls_b,ls_c]
print(output)

Comments

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.