Lists - tuple unpacking

0 Tia Blake · October 26, 2015
I have a list with cities and coordinates.

City = [
    ('Boston', 42.21, 71.03), ('Los Angeles', 34.03, 118.15) ...

 I would also like to learn what i need to do to get the name of the city after i'm asked for coordinate1 and coordinate2.

I would be really grateful if anyone decided to help.:)

Post a Reply


Oldest  Newest  Rating
+1 alex constantin · October 26, 2015
To unpack the city list, you would do:

city = [('Boston', 42.21, 71.03), ('Los Angeles', 34.03, 118.15)]
coord1, coord2 = city

Here, coord1 would be equal to ('Boston', 42.21, 71.03) and coord2,  ('Los Angeles', 34.03, 118.15).

Now, if you have coord1, to access it you would do:
coord1city = coord1[0]

because it is the first element in the tuple.

If you want to access it without unpacking, you can do:
bostonCity = city[0][0]
  • 1



This section is all about snakes! Just kidding.

Bucky Roberts Administrator