I'm still a beginner at Python. Here is my list:
['Kaivopuisto', ('24.950292890004903', '60.155444793742276'),
'Laivasillankatu', ('24.956347471358754', '60.160959093887129'),
'Kapteeninpuistikko', ('24.944927399779715', '60.158189199971673'),
'Viiskulma', ('24.941775800312996', '60.16098589997938'),
'Sepankatu', ('24.93628529982675', '60.157948300373846'),
'Hietalahdentori', ('24.929709900391629', '60.162225100108344'),
'Designmuseo', ('24.945959999554361', '60.163103199952786'),
'Vanha kirkkopuisto', ('24.939149900447603', '60.165288299815245'),
'Erottajan aukio', ('24.944134928883898', '60.166911666939939')]
I need to create a new dictionary with the string (name) as the key and the tuple as the value. For clarification, the string is the name of a city in Finland and the tuple is longitude and latitude for the city. I really could use some guidance please.
I tried list comprehension, I tried a for loop, I tried the update method - I can't get anything to work.
You need to iterate over your list and skip every other item so that you only grab the cities name as the key, then you can use i+1 to grab the coordinates
cities = [
'Kaivopuisto', ('24.950292890004903', '60.155444793742276'),
'Laivasillankatu', ('24.956347471358754', '60.160959093887129'),
'Kapteeninpuistikko', ('24.944927399779715', '60.158189199971673'),
'Viiskulma', ('24.941775800312996', '60.16098589997938'),
'Sepankatu', ('24.93628529982675', '60.157948300373846'),
'Hietalahdentori', ('24.929709900391629', '60.162225100108344'),
'Designmuseo', ('24.945959999554361', '60.163103199952786'),
'Vanha kirkkopuisto', ('24.939149900447603', '60.165288299815245'),
'Erottajan aukio', ('24.944134928883898', '60.166911666939939')
]
# Create an empty dictionary
cities_dict = {}
# Step is 2 so that we skip the coordinates and only grab the cities as the key
for i in range(0, len(cities), 2):
name = cities[i]
coordinates = cities[i+1]
cities_dict[name] = coordinates
print(cities_dict)