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

How to display/create a variable with a filtered dictionary item

I have a database with 8 entry lines. The dictionary was created using a .csv file created with Excel.

Here’s what I am using to try to call on filtered items in result:

import csv
#Import a CSV with lines of relevant person's information to a dictionary
reader = csv.reader(open('NameGenderSport.csv'))

result = {}
for row in reader:
    key = row[0]
    if key in result:
        # implement your duplicate row handling here
        pass
    result[key] = row[1:]
print(result)

print(result.items())
parsedData = {k:v for k,v in result.items() if k == "Basketball"}
print(parsedData)

Here is the dictionary (result):

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

{'Peter': ['Male', 'Basketball'], 'Carlos': ['Male', 'Tennis'], 'Angela': ['Female', 'Golf'], 'David': ['Male', 'Basketball'], 'Kaitlin': ['Female', 'Golf'], 'Marco': ['Male', 'Tennis'], 'Andrew': ['Male', 'Golf'], 'Carmen': ['Female', 'Basketball']}

The desired output should only list the full information of the dictionary entry’s that play Basketball:

Name: Peter 
Gender: Male
Sport: Basketball

Name: David 
Gender: Male
Sport: Basketball

Name: Carmen 
Gender: Female
Sport: Basketball

Any sort of direction would be amazingly helpful! Should I be using Classes? or some other methodology?

>Solution :

dict.items() gives key, value pairs, so in your dictionary comprehension k are keys (in this case, the people’s names) and v are values (the lists of genders and sports). So the if needs to check if Basketball is in the values, as indicated by the previous answer. It will never be the case that k == "Basketball" because k are the people’s names.

Since you know that your dictionary contains people’s names as keys and other information as values, it will make for clearer code if you assign descriptive names like name and info when iterating over result.items().

It looks like you want to get a string as output. You could use the following list comprehension with join.

result = {'Peter': ['Male', 'Basketball'], 'Carlos': ['Male', 'Tennis'], 'Angela': ['Female', 'Golf'], 'David': ['Male', 'Basketball'], 'Kaitlin': ['Female', 'Golf'], 'Marco': ['Male', 'Tennis'], 'Andrew': ['Male', 'Golf'], 'Carmen': ['Female', 'Basketball']}

parsedData = [
    '\n'.join([f"Name: {name}", f"Gender: {info[0]}", f"Sport: {info[1]}"])
    for name, info in result.items()
    if info[1] == "Basketball"
]
print('\n\n'.join(parsedData))

Output:

Name: Peter
Gender: Male
Sport: Basketball

Name: David
Gender: Male
Sport: Basketball

Name: Carmen
Gender: Female
Sport: Basketball
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