Home > Back-end >  How to display/create a variable with a filtered dictionary item
How to display/create a variable with a filtered dictionary item

Time:09-07

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):

{'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?

CodePudding user response:

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

CodePudding user response:

You were almost there.

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']}

print([(k,v) for k,v in result.items() if "Basketball" in v])
>> [('Peter', ['Male', 'Basketball']),
>>  ('David', ['Male', 'Basketball']),
>>  ('Carmen', ['Female', 'Basketball'])]
  • Related