Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Group repeated items in a list and the same index of other list in Python

I am pretty new to Python and I am working on this problem where I need to get items from the same index of bunch of lists:

name_list = ["trung", "key", "trung","kayla","john","trung","key"]
id_list  = ["123", "456", "789", "852", "545", "915", "712"]
summary_list = ["aa12", "bb99", "cc66", "dd854", "ee842", "ff723", "gg413"]
version_list = ["a01", "a22", "a23", "a94", "a05", "a42", "a34"]

I want to get the column of all the repeated name and their respective data

Name: Trung
id_list = ["123", "789", "915"]
summary_list = ["aa12", "cc66", "ff723"]
version_list = ["a01", "a23", "a42"]

Name: key
id_list = ["456", "712"]
summary_list = ["bb99", "gg413"]
version_list = ["a22", "a34"]

Name: kayla
id_list = ["852"]
summary_list = ["dd854"]
version_list = ["a94"]

Name: john
id_list = ["545"]
summary_list = ["ee842"]
version_list = ["a05"]

What I have tried was:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for i in range(len(name_list)):
for name in name_list:
    if(name == name_list[i]):
        print("Hi ", name, "your id is: ", id_list[i], ". Part number: ", summary_list[i], ". And version: ", version_list[i])

but it printed out way too many lines

Hi  trung your id is:  123 . Part number:  aa12 . And version:  a01 

Hi  trung your id is:  123 . Part number:  aa12 . And version:  a01 

Hi  trung your id is:  123 . Part number:  aa12 . And version:  a01 

Hi  key your id is:  456 . Part number:  bb99 . And version:  a22 

Hi  key your id is:  456 . Part number:  bb99 . And version:  a22 

Hi  trung your id is:  789 . Part number:  cc66 . And version:  a23 

Hi  trung your id is:  789 . Part number:  cc66 . And version:  a23 

Hi  trung your id is:  789 . Part number:  cc66 . And version:  a23 

Hi  kayla your id is:  852 . Part number:  dd854 . And version:  a94 

Hi  john your id is:  545 . Part number:  ee842 . And version:  a05 

Hi  trung your id is:  915 . Part number:  ff723 . And version:  a42 

Hi  trung your id is:  915 . Part number:  ff723 . And version:  a42 

Hi  trung your id is:  915 . Part number:  ff723 . And version:  a42 

Hi  key your id is:  712 . Part number:  gg413 . And version:  a34 

Hi  key your id is:  712 . Part number:  gg413 . And version:  a34 

Another way I tried but it only return with all the one that is repeat more than 1 time

id = []
for i in range(len(name_list)):
    if(name_list.count(name_list[i]) > 1):
        print(name_list[i], id_list[i], summary_list[i], version_list[i])
        id.append(id_list[i])
print(id)  

Result

trung 123 aa12 a01
key 456 bb99 a22
trung 789 cc66 a23
trung 915 ff723 a42
key 712 gg413 a34
['123', '456', '789', '915', '712']

How do I get the script to understand and separate the list? Thank you for reading and helping.

>Solution :

Try using a collection.defaultdict and iterate over name_list, adding entries for each corresponding entry in the other lists. Like this:

import collections

name_list = ["trung", "key", "trung","kayla","john","trung","key"]
id_list  = ["123", "456", "789", "852", "545", "915", "712"]
summary_list = ["aa12", "bb99", "cc66", "dd854", "ee842", "ff723", "gg413"]
version_list = ["a01", "a22", "a23", "a94", "a05", "a42", "a34"]

# Group by name
dct = collections.defaultdict(lambda: {'id': [], 'summary': [], 'version': []})
for i, name in enumerate(name_list):
    dct[name]['id'].append(id_list[i])
    dct[name]['summary'].append(summary_list[i])
    dct[name]['version'].append(version_list[i])

# Print it out
for name in dct:
    print(f'Name: {name}')
    print(f'id_list = {dct[name]["id"]}')
    print(f'summary_list = {dct[name]["summary"]}')
    print(f'version_list = {dct[name]["version"]}')
    print()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading