Linked Questions

251 votes
10 answers
269k views

Sorting a Python list by two fields [duplicate]

I have the following list created from a sorted csv list1 = sorted(csv1, key=operator.itemgetter(1)) I would actually like to sort the list by two criteria: first by the value in field 1 and then by ...
half full's user avatar
  • 2,765
92 votes
6 answers
134k views

How to sort a list by length of string followed by reverse alphabetical order? [duplicate]

Given a list of words, return a list with the same words in order of length (longest to shortest), the second sort criteria should be alphabetical. Hint: you need think of two functions. This is what ...
Adrian's user avatar
  • 7,951
65 votes
1 answer
79k views

Sort a list of tuples depending on two elements [duplicate]

I have a list of tuples, like so: [ ('a', 4, 2), ('a', 4, 3), ('a', 7, 2), ('a', 7, 3), ('b', 4, 2), ('b', 4, 3), ('b', 7, 2), ('b', 7, 3) ] I know that, for example to sort them by the ...
Razer's user avatar
  • 8,103
0 votes
5 answers
14k views

Sort list of strings by length and alphabetically [duplicate]

I need to sort a list of words based on two criteria given. I need to return a list with the same words in order of length (longest to shortest) and the second sort criteria should be alphabetical. ...
John's user avatar
  • 33
7 votes
1 answer
9k views

Sort list of dicts by multiple values [duplicate]

I have the following dict tempdata = {'logs': [ {'starttime':'901', 'time':'5' }, {'starttime':'902', 'time':'3' }, {'starttime':'900', 'time':'2' }, ...
Islarf's user avatar
  • 159
8 votes
1 answer
11k views

Python: Sort a list of objects based on their attributes [duplicate]

Just to preface this, I've already checked out posts pertaining to this question and they haven't fully answered mine. So I would just like to know how to sort a list of objects based on their ...
SirGoose's user avatar
  • 129
6 votes
2 answers
2k views

Length-wise-sorted list but, same length in alphabetical-order in a step [duplicate]

My Python List of string is something like x but long enough: x = ['aaa','ab','aa','c','a','b','ba'] I wants to sort this list as: ['a', 'b', 'c', 'aa', 'ab', 'ba', 'aaa'] and I did as ...
Grijesh Chauhan's user avatar
5 votes
1 answer
9k views

lambda function with multiple variables in sorting [duplicate]

There is a class with two domains of value. I want sort a list of such pairs with following criteria: If the first domain are different, sort by first domain; Otherwise, sort by second domain. It is ...
user avatar
0 votes
3 answers
3k views

Sort a dict according to both key and value at the same time [duplicate]

I have a dict that looks like this: {0: 2, 1: 4, 2: 2, 3: 2, 4: 5} That is, both the keys and values integers. I need to sort this dict in this way: By the value first, ascending; by the key, if the ...
Memphis Meng's user avatar
  • 1,581
0 votes
2 answers
4k views

Python Sort Multidimensional List first element Based on second element that already sorted [duplicate]

I have a multidimensional List like this (the second element already sorted) [[92, 25], [93, 25], [95, 27], [94, 27], [94, 27], [92, 27], [89, 27], [89, 27], [92, 27], [91, 30], [90, 30], [90, 30]...
Wailan Tirajoh's user avatar
0 votes
2 answers
425 views

Sorting list of list based on Inner List's Multiple Integer elements [duplicate]

I want to sort the list of list based on the two factors: PRIMARILY: list has to be sorted in ascending order based on the second element of each list in the list If there are cases wheres various ...
p.ram's user avatar
  • 109
1 vote
2 answers
282 views

Python: How do I sort on the second item of a tuple? [duplicate]

I have this piece of code relations = dict() relations[('a','b','c')] = 'first' relations[('a','c','c')] = 'third' relations[('d','b','c')] = 'second' relations_sorted = sorted(relations.keys(), key=...
gctwnl's user avatar
  • 239
-1 votes
1 answer
656 views

How to sort a list in python first depending on a value, and then depending on another value? [duplicate]

I have a python list as: arr = [('f', 1), ('g', 2), ('h', 3), ('a', 1), ('j', 1), ('o', 1), ('v', 2)] I want to sort this list in this way: arr = [('h', 3), ('g', 2), ('v', 2), ('a', 1), ('f', 1), ('...
Debtanu Gupta's user avatar
0 votes
1 answer
257 views

Sorting 2 columns at once [duplicate]

Im trying to sort these data in : - in alphabetical order with each person's highest score for the tests - by the highest score
ImYu's user avatar
  • 13
-1 votes
3 answers
267 views

Sorting nested lists in python [duplicate]

I have a nested list list_a = [['1 order', 15, 3 ], ['1 order', 29, 2], ['1 order', 5, 1], ['2 order', 2, 3], ['2 order', 1, 2], ['2 order', 17, 1], ['3 order', 2, 3], ['3 order', 10, 2], ['3 order',...
john's user avatar
  • 11

15 30 50 per page
1
2 3 4 5 6