Mohd Fairuz
Mohd Fairuz

Reputation: 25

How to reverseTuples defined in list?

I don't know how to explain this but I will try. I have pre-defined tuples and a list which contains those tuples:

EL = (42.7358, -84.4844)
Det = (42.3831, -83.1022)
Kal = (42.2747, -85.5883)
AA = (42.2753, -83.7308)
Lan = (42.7092, -84.5539)
GR = (42.9614, -85.6558)
SSM = (46.4844, -84.3656)

cities = [EL, Det, Kal, AA, Lan, GR, SSM]
  1. The cities is in (x, y) order. How can I swap the order to (y, x) and saving it as cities2.

  2. How can I reverse the order of the elements in cities? For example, it will be like this:

    cities3 = [SSM, GR, Lan, AA, Kal, Det, EL]
    

I've tried cities.reverse(), but it reversed the coordinate. Any hints and helps would be much appreciated.

Thanks guys.

Upvotes: 1

Views: 52

Answers (2)

attdona
attdona

Reputation: 18943

You are right about reversing the items:

cities.reverse()

reverse does the job, reversing in place the items (it modifies cities):

Since you have tuples of coordinates, it is not possible to swap in place (x,y) but ,if you want to create another list, list comprehensions is one of the right tools to use.

If you want swap (x,y) to (x,y):

cities_swap_xy = [(y,x) for (x,y) in cities]

to create another list with reversed order:

cities_reverse = cities[-1:0:-1]

to swap coordinates and reverse city:

cities_reverse_and_swap_xy = [(y,x) for (x,y) in cities[-1:0:-1]]

There is a lot of documentation about list comprehensions, good learning!

Upvotes: 2

Paritosh Singh
Paritosh Singh

Reputation: 6246

You have a list of tuples. Swapping tuples is simply a matter of accessing the 1st index in the tuple before 0 index and storing the output.

EL = (42.7358, -84.4844)
Det = (42.3831, -83.1022)
Kal = (42.2747, -85.5883)
AA = (42.2753, -83.7308)
Lan = (42.7092, -84.5539)
GR = (42.9614, -85.6558)
SSM = (46.4844, -84.3656)

cities = [EL, Det, Kal, AA, Lan, GR, SSM]
print(cities)
#Output:
[(42.7358, -84.4844),
 (42.3831, -83.1022),
 (42.2747, -85.5883),
 (42.2753, -83.7308),
 (42.7092, -84.5539),
 (42.9614, -85.6558),
 (46.4844, -84.3656)]


cities2 = [(x[1], x[0]) for x in cities]
print(cities)
#Output:
[(-84.4844, 42.7358),
 (-83.1022, 42.3831),
 (-85.5883, 42.2747),
 (-83.7308, 42.2753),
 (-84.5539, 42.7092),
 (-85.6558, 42.9614),
 (-84.3656, 46.4844)]

As for reversing a list, there are many ways to reverse a list. some create a copy, others modify the original list, such as .reverse()

cities3 = cities[::-1] #created a reversed copy.
#Alternatively
cities3 = list(reversed(cities))

Lastly however, a recommendation.

Your lists of tuples as-is do not make a lot of sense, but the name labels give the tuples meaning.

You should consider a different data structure to store this information,and dictionaries in this case makes sense. You can think of them as mappings between key:value pairs.

EL = (42.7358, -84.4844)
Det = (42.3831, -83.1022)
Kal = (42.2747, -85.5883)
AA = (42.2753, -83.7308)
Lan = (42.7092, -84.5539)
GR = (42.9614, -85.6558)
SSM = (46.4844, -84.3656)

cities = [EL, Det, Kal, AA, Lan, GR, SSM]
city_names = ["EL", "Det", "Kal", "AA", "Lan", "GR", "SSM"]
cities_dict = dict(zip(city_names, cities))

Note that the above code is equivalent the following:

cities_dict = {"EL": EL,
          "Det": Det,
          "Kal": Kal,
          "AA": AA,
          "Lan": Lan,
          "GR": GR,
          "SSM": SSM
        }

print(cities_dict)
#Output:
{'EL': (42.7358, -84.4844),
 'Det': (42.3831, -83.1022),
 'Kal': (42.2747, -85.5883),
 'AA': (42.2753, -83.7308),
 'Lan': (42.7092, -84.5539),
 'GR': (42.9614, -85.6558),
 'SSM': (46.4844, -84.3656)}

Now, a caveat, dictionaries are unordered inherently. If you want a dict-like data structure that supports ordering, use an OrderedDict.

from collections import OrderedDict
cities = [EL, Det, Kal, AA, Lan, GR, SSM]
city_names = ["EL", "Det", "Kal", "AA", "Lan", "GR", "SSM"]
cities_dict = OrderedDict(zip(city_names, cities))
print(cities_dict)

cities2_dict = OrderedDict((key, (v[1], v[0])) for (key,v) in cities_dict.items())
print(cities2_dict)

cities3_dict = OrderedDict(reversed(cities_dict.items()))
print(cities3_dict)

Upvotes: 0

Related Questions