Python Forum
to find in dictionary given parameter 'name' and to output position
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
to find in dictionary given parameter 'name' and to output position
#11
My Ergebnis is a list of dictionaries.

Assuming there is a result, you get the list Ergebnis, for a name like Swensonl. Each element in the list is dictionary.

Ergebnis = findName(person)
if not Ergebnis == 'Nix da!':
    # Ergebnis is a list, walk through it
    for E in Ergebnis:
        print(E)
I get this output when I enter the surname Swenson:

Output:
{'vorname': 'Thom', 'name': 'Swenson', 'adress': 'bethastr. 37'} {'vorname': 'Dumb', 'name': 'Swenson', 'address': 'Gottlobstr. 99'}

You could do that like this:

# Ergebnis is a list of dictionaries
if not Ergebnis == 'Nix da!':
     # E is a dictionary
     for E in Ergebnis:
          for key in E.keys():
               print('key is', key, 'value is', E[key])
Your searchName(telefonbook, name) function works but returns a dictionary of dictionaries, if the surname is present more than 1 time, as in my telefonbook.

def searchName(telefonbook, name):
     """Durchsucht ein Adressbuch nach einem Namen."""   
     results = {}
    # item is a tuple of (key, value) 
     for item in telefonbook.items():
         person = item[1]
         if name in person.values():
             results[item[0]] = person
     return results
So then do:

Resultat = searchName(telefonbook, name)

for key in Resultat.keys():
     print('key is', key, 'data is', Resultat[key])
Wer suchet, der findet!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  dictionary output to text file (beginner) Delg_Dankil 2 1,187 Jul-12-2023, 11:45 AM
Last Post: deanhystad
  Using dictionary to find the most sent emails from a file siliusu 6 7,588 Apr-22-2021, 06:07 PM
Last Post: siliusu
  From string parameter to a dictionary Mitchie87 9 3,128 Oct-12-2019, 10:34 PM
Last Post: Larz60+
  how to find 'cycle' for key-value pairs in a dictionary? junnyfromthehood 1 3,585 Sep-29-2019, 01:07 AM
Last Post: ichabod801
  Powerball assignment, how to get correct output of a dictionary ? msp1981 5 3,290 Mar-19-2019, 11:02 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020