Python Forum
Sorting A List of Tuples Question
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting A List of Tuples Question
#1
Hi... I am relatively new to Python.  I am trying to sort a list of tuples - each tuple contains a boolean variable and 
a string.  I'd like to use the list sort() method... I'm a little confused if I need to use a lambda or use key= or maybe 
even cmd=  I'd like to really do a comparison like cmp((is_present, element.text), (is_present, element.text)) or 
something similar to compare the tuples.  I'm just starting out in my journey with Python so any help or advice 
you can give would be great.
Reply
#2
You can use .sort() to sort list of tuples without any parameters. Python sorts list of tuples lexicographically - at first it sorts it by first elements in tuples, then by second elements (if needed) and so on

So your tuples (bool, str) will be sorted by bool first (all tuples starting with False will be before any tuple starting with True) and then by str. If you need different sorting, you can use key parameter with either lambda or with other function (builtin or your own) - sort applies key function on items and sort is done by returned values.

Example:
Output:
In [1]: my_list = [(False, 'word'), (False, 'another word'), (True, 'third_word'), (True, "Uppercase word"), (False, "Word Uppercase")] In [2]: my_list.sort()   # "natural" tuple sort In [3]: print(my_list) [(False, 'Word Uppercase'), (False, 'another word'), (False, 'word'), (True, 'Uppercase word'), (True, 'third_word')] In [4]: my_list.sort(key=lambda x: (x[0], x[1].lower()) )   # case-insensitive sort In [5]: print(my_list) [(False, 'another word'), (False, 'word'), (False, 'Word Uppercase'), (True, 'third_word'), (True, 'Uppercase word')] In [6]: my_list.sort(key=lambda x: len(x[1]))   # sorted just by length of string In [7]: print(my_list) [(False, 'word'), (True, 'third_word'), (False, 'another word'), (False, 'Word Uppercase'), (True, 'Uppercase word')]
Reply
#3
That is great... thanks so much.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding values with reduce() function from the list of tuples kinimod 10 2,658 Jan-24-2023, 08:22 AM
Last Post: perfringo
  List Sorting Problem ZZTurn 5 1,340 Sep-22-2022, 11:23 PM
Last Post: ZZTurn
  Sorting List finndude 9 2,460 Jan-27-2022, 09:37 PM
Last Post: Pedroski55
  Simple code question about lambda and tuples JasPyt 7 3,321 Oct-04-2021, 05:18 PM
Last Post: snippsat
  sorting a list of lists by an element leapcfm 3 1,866 Sep-10-2021, 03:33 PM
Last Post: leapcfm
Question convert unlabeled list of tuples to json (string) masterAndreas 4 7,462 Apr-27-2021, 10:35 AM
Last Post: masterAndreas
  Count number of occurrences of list items in list of tuples t4keheart 1 2,381 Nov-03-2020, 05:37 AM
Last Post: deanhystad
  Need help improving function that reads file into list of tuples t4keheart 6 3,056 Nov-03-2020, 04:58 AM
Last Post: ndc85430
  basic question about tuples and immutability sudonym3 6 2,898 Oct-18-2020, 05:11 PM
Last Post: sudonym3
  Sorting list of names using a lambda (PyBite #5) Drone4four 2 2,732 Oct-16-2020, 07:30 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020