-8

I have a list of tuples or a tuple of tuples and I want to swap the tuples like in the next example:

>>> swap([('abc', 'bcd'), ('efg', 'zza')])
[('efg', 'zza'), ('abc', 'bcd')]

I want to swap the tuples from the list based on the third character of the second element of every tuple.

  • Try `new_list = list(reversed(**put original list here**)` – Rob Oct 13 '17 at 22:03
  • Try reversing the list `mylist[::-1]`. – randomir Oct 13 '17 at 22:04
  • Or list_name[::-1] – jadsq Oct 13 '17 at 22:04
  • Are you trying to reverse the list, or swap the values? In your real list, do both tuples start with `'abc'`? – Adam Smith Oct 13 '17 at 22:05
  • What exactly is everything that you've found (and tried)? – CristiFati Oct 13 '17 at 22:06
  • Or: better question. If your test data was `[(1, 2), (3, 4)]` would your intended result be `[(3, 4), (1, 2)]` or `[(1, 3), (2, 4)]` (or `[(1, 4), (3, 2)]`)? – Adam Smith Oct 13 '17 at 22:06
  • Well my problem is that i have more than 2 elements in my main list, and i need to swap them all... – Apetrei Alin Oct 13 '17 at 22:07
  • @AdamSmith yes both of them start with 'abc', it's about swaping them based on the 3rd character of the 2nd element in the tuple – Apetrei Alin Oct 13 '17 at 22:08
  • @ApetreiAlin your intended functionality is unclear. On what basis is the 3rd character of the 2nd element of the tuple compared? You'll need to be *much* more specific. Give us some real data. – Adam Smith Oct 13 '17 at 22:10
  • Well in this example we compare 'd' from the second element 'bcd' with 'a' from 'zza', i hope u get it now. I have *args parameters, so something like args[0][1][2] compare with args[1][1][2] – Apetrei Alin Oct 13 '17 at 22:12
  • @ApetreiAlin so what you really want is to sort the list by the 3rd character of the second element of each tuple? – Adam Smith Oct 13 '17 at 22:13
  • Yea... sorry, sometimes i am not so clear – Apetrei Alin Oct 13 '17 at 22:14
  • (sorry to duplicate close voters, but it became obvious that the question he was asking was not the *question he asked*. The latter was exactly the dupe target. The former is a different dupe, I'm sure, but I don't have it handy.) – Adam Smith Oct 13 '17 at 22:19
  • @MosesKoledoye courtesy ping. I asked about this [on meta](https://meta.stackoverflow.com/questions/357824/should-i-use-my-dupehammer-to-open-an-xy-problem) – Adam Smith Oct 13 '17 at 22:43

1 Answers1

2

Hoo boy, this was a big XY problem. You want to sort by the third character of the second element of each tuple. Use the sorted built-in with its kwarg key.

lst = [('abc', 'bcd'), ('abc', 'zza')]
result = sorted(lst, key=lambda t: t[1][2])

key here accepts a function that is given each element in turn, and does something to it that is compared. In this case it returns the second element's third element (t[1][2]). Each element of the outer list is a tuple. Each tuple's second element exists in index 1, each second element's third character exists in index 2.

Adam Smith
  • 52,157
  • 12
  • 73
  • 112