1

I have a bunch of tuples:

Stadt = ("Berlin", "Santiago", "Madrid", "Dallas", "Mexico")
Befolkerung = (3.8, 5.6, 6.6, 1.34, 8.86)
Land = ("Deutschland", "Chile", "Spanien", "Vereinigte Staaten", "Mexico")
Breitengrad = (1,2,3,4,5)
Langengrad = (2,4,6,8,10)

I would like to create a dictionary from these with Stadt as keys and the rest of the tuples as values. How would you go about this?

Zweifler
  • 375
  • 1
  • 4
  • 12

4 Answers4

4
dict(zip(Stadt, zip(Befolkerung, Land, Breitengrad, Langengrad)))
Grigory Feldman
  • 405
  • 3
  • 7
4

Use zip with dict

Ex:

Stadt = ("Berlin", "Santiago", "Madrid", "Dallas", "Mexico")
Befolkerung = (3.8, 5.6, 6.6, 1.34, 8.86)
Land = ("Deutschland", "Chile", "Spanien", "Vereinigte Staaten", "Mexico")
Breitengrad = (1,2,3,4,5)
Langengrad = (2,4,6,8,10)

print(dict(zip(Stadt, zip(Befolkerung, Land, Breitengrad, Langengrad))))

Output:

{'Berlin': (3.8, 'Deutschland', 1, 2),
 'Dallas': (1.34, 'Vereinigte Staaten', 4, 8),
 'Madrid': (6.6, 'Spanien', 3, 6),
 'Mexico': (8.86, 'Mexico', 5, 10),
 'Santiago': (5.6, 'Chile', 2, 4)}
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

I am not sure about your exact requirement regarding keys:

Following code has multiple keys: Stadt, Befolkerung, Land, Breitengrad, Langengrad

dictionary={
    'Stadt':Stadt,
    'Befolkerung':Befolkerung,
    'Land':Land,
    'Breitengrad':Breitengrad,
    'Langengrad':Langengrad
} 
print(dictionary)

output:

{
 'Stadt': ('Berlin', 'Santiago', 'Madrid', 'Dallas', 'Mexico'),
 'Befolkerung': (3.8, 5.6, 6.6, 1.34, 8.86),
 'Land': ('Deutschland', 'Chile', 'Spanien', 'Vereinigte Staaten', 'Mexico'),
 'Breitengrad': (1, 2, 3, 4, 5),
 'Langengrad': (2, 4, 6, 8, 10)
}
The Guy
  • 411
  • 4
  • 11
-1
yourdict = {}
Stadt = ("Berlin", "Santiago", "Madrid", "Dallas", "Mexico")
Befolkerung = (3.8, 5.6, 6.6, 1.34, 8.86)
Land = ("Deutschland", "Chile", "Spanien", "Vereinigte Staaten", "Mexico")
Breitengrad = (1,2,3,4,5)
Langengrad = (2,4,6,8,10)
for key in list(locals().keys()):
    if(not key.startswith('__')):
        yourdict[key] = locals()[key]
print(yourdict)
Manivannan Murugavel
  • 1,476
  • 17
  • 14