I have a bunch of items that are associated with different groups, and I ultimately want to create a list for each group, containing all associated items.
The catch is I do not know how many groups there are, so how can I dynamically generate the correct number of lists, as well as how to call on them?
I am looping through item_list
and group_list
, two different series that allign perfectly with each other, as in item_list[item]
has the corresponding group in group_list[item]
Here is some raw data:
item list group list
A 1
B 1
C 2
D 1
E 2
F 1
G 2
H 2
I 1
J 2
This is what I have so far:
groups = []
for item in item_list:
groups.append(group_list[item])
# Get only unique values (instead of having groups 1,1,1,2,2 --> 1,2)
group_set = list(set(groups))
# Number of lists that need to be generated
len(group_set)
What I want to end up with:
[IN]: print list_1:
[OUT]: ['A', 'B', 'D', 'F', 'I']
[IN]: print list_2:
[OUT]: ['C', 'E', 'G', 'H', 'J']
where list_1 and list_2 was generated because len(group_set) from my current code is equal to 2.
I'm just not sure how to dynamically generate that number of lists, and put each item in the appropriate list.
Any advice/guidance is much appreciated...