From what I understood, what you essentially want to do is flatten your list while keeping unique items.
The unique items can be achieved by converting the list
into set
and then back to list
.
The unpacking part is really well explained in this post. Here's a working code for you -
df_dict = {
'Group1': [[1,2,3,4], [1, 2, 5, 6 ]],
'Group2': [[1,2], [2,3],[2,3,4]]
}
final_dict = {}
for k, v in df_dict.items():
# flatten the list using double list comprehension
flat_list = [item for sublist in v for item in sublist]
final_dict[k] = list(set(flat_list))
This gives the final_dict
as -
{'Group1': [1, 2, 3, 4, 5, 6], 'Group2': [1, 2, 3, 4]}
Please tell me if this answers your query.
Edit for Integer values in between the lists -
If we have a list with integer values in between, then you will get the int object not iterable error
, to solve it we can check the instance to be int
and make the item list by ourselves
Working code -
df_dict = {
'Group1': [[1,2,3,4], 3, [1, 2, 5, 6 ]],
'Group2': [[1,2], [2,3],[2,3,4]],
}
final_dict = {}
for k, v in df_dict.items():
# making a new list
new_list = []
for item in v:
# for int we will convert it to 1 length list
if isinstance(item, int):
item = [item]
for ele in item:
new_list.append(ele)
final_dict[k] = list(set(new_list))
final_dict
Final dict -
{'Group1': [1, 2, 3, 4, 5, 6], 'Group2': [1, 2, 3, 4]}
As expected