Home > Software engineering >  Displaying List values in a Dictionary
Displaying List values in a Dictionary

Time:12-12

I want to create a dictionary where the key is the athlete’s first and last name stored as a tuple, and the value is a list containing the athlete’s country and finishing time in hours, minutes and seconds (in a tuple) as shown below:

dict={(Richard,Ringer):[Germany,(2,11,27)],
(Eliud,Kipchoge):[Kenya,(2,8,38)],
(Yavuz,Agrali):[Turkey,(2,15,5)],
...}

I managed to display the name and surname since it is in a tuple by creating a simple for loop, but I have no idea how can I display the list part.

for i in dict:
        name=i[0]
        surname=i[1]
        print(name ' ' surname)

Is there a similar way to print list of items in a dictionary?

CodePudding user response:

data={('Richard','Ringer'):['Germany',(2,11,27)],('Eliud','Kipchoge'):['Kenya',(2,8,38)],('Yavuz','Agrali'):['Turkey',(2,15,5)]}

info = []
for item in dict(data):
    info.append({
        'name': item[0],
        'surname': item[1],
        'country': data[item][0],
        'finishing_time': {
            "hours": data[item][1][0],
            "minutes": data[item][1][1],
            "seconds": data[item][1][2],
        }
    })
print(info);

Output :-

[{'name': 'Richard',
  'surname': 'Ringer',
  'country': 'Germany',
  'finishing_time': {'hours': 2, 'minutes': 11, 'seconds': 27}},
 {'name': 'Eliud',
  'surname': 'Kipchoge',
  'country': 'Kenya',
  'finishing_time': {'hours': 2, 'minutes': 8, 'seconds': 38}},
 {'name': 'Yavuz',
  'surname': 'Agrali',
  'country': 'Turkey',
  'finishing_time': {'hours': 2, 'minutes': 15, 'seconds': 5}}]
  • Related