Python Forum
sorting a list of lists by an element
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sorting a list of lists by an element
#1
Hi,

I have a list of lists that I want to sort, something like :

data = [[0,False,'c'], [0,True,'z'], [1,False,'P'], [0,True,'A']]
I want all lists that have a True value at the top of Data, like :

sortingFuntion(data) = [[0,True,'z'], [0,True,'A'], [0,False,'c'], [1,False,'P']]
I can access the boolean with Data[i][1] in a for loop but I can't come up with how to transmit that value as a criteria to sorted()

Thanks
Yoriz write Sep-10-2021, 03:16 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Use sorted with a custom key function
data = [[0, False, "c"], [0, True, "z"], [1, False, "P"], [0, True, "A"]]
sorted_data = sorted(data, key=lambda x: x[1], reverse=True)
print(sorted_data)
Output:
[[0, True, 'z'], [0, True, 'A'], [0, False, 'c'], [1, False, 'P']]

Instead of using lamda you can also use itemgetter from the operator module
from operator import itemgetter

data = [[0, False, "c"], [0, True, "z"], [1, False, "P"], [0, True, "A"]]
sorted_data = sorted(data, key=itemgetter(1), reverse=True)
print(sorted_data)
Reply
#3
And for the folks that don't like lambdas, in this case itemgetter is possible as well.

from operator import itemgetter
[...]
sorted_data = sorted(data, key=itemgetter(1), reverse=True)
Reply
#4
Thanks a lot !

I am fairly new to programming, will look up the lamda and itemgetter functions
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  list in dicitonary element problem jacksfrustration 3 625 Oct-14-2023, 03:37 PM
Last Post: deanhystad
  Need help with sorting lists as a beginner Realist1c 1 712 Apr-25-2023, 04:32 AM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt
  user input values into list of lists tauros73 3 1,025 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  Find (each) element from a list in a file tester_V 3 1,156 Nov-15-2022, 08:40 PM
Last Post: tester_V
  returning a List of Lists nafshar 3 1,014 Oct-28-2022, 06:28 PM
Last Post: deanhystad
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,717 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 1,564 Oct-01-2022, 07:15 PM
Last Post: Skaperen
  List Sorting Problem ZZTurn 5 1,281 Sep-22-2022, 11:23 PM
Last Post: ZZTurn
  Membership test for an element in a list that is a dict value for a particular key? Mark17 2 1,162 Jul-01-2022, 10:52 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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