1

I have a list of lists like the following:

list_of_lists = [
   ('test_ss', 'Test 1'),
   ('test_2_ss', 'Test 2'),
   ('test_3_ss', 'Test 3'),
   ('test_ss', 'Test 4')
]

I need to sort this list of lists by the first item in each list based on a given variable string.

As an example, I want to sort by 'test_ss' to the resulting list of lists would be:

sorted_list_of_lists = [
   ('test_ss', 'Test 1'),
   ('test_ss', 'Test 4'),
   ('test_2_ss', 'Test 2'),
   ('test_3_ss', 'Test 3'),
]

I've tried a number of examples off SO and others (Sorting a list of lists based on a list of strings, Sorting lists based on a particular element - Python, sorting multiple lists based on a single list in python, etc) but haven't found the right approach (or I've just not been following those examples correctly.

Any pointers?

9
  • @arman - thanks for the edit...I thought I did it right but apparently I did not :) Commented Jan 5, 2016 at 21:24
  • What if the first string of two elements is equal, is the sorting arbitrary, or do you want to introduce a tie breaker? Commented Jan 5, 2016 at 21:26
  • apparently you want some kind of a natural sort, since test_2_ss should come before test_ss (since 2's code is lower than s'). Do sorted(list_of_lists) and see it for yourself Commented Jan 5, 2016 at 21:27
  • 3
    What does it mean "sort by 'test_ss'", sort by the first position? Commented Jan 5, 2016 at 21:28
  • Eric, please answer @karakfa's comment Commented Jan 5, 2016 at 21:48

3 Answers 3

2

You can use a simple key function like this:

In [59]: def compare(element):
   ....:     return element[0] == 'test_ss' or 99999
   ....: 

In [60]: sorted(list_of_lists, key=compare)
Out[60]: 
[('test_ss', 'Test 1'),
 ('test_ss', 'Test 4'),
 ('test_2_ss', 'Test 2'),
 ('test_3_ss', 'Test 3')]
Sign up to request clarification or add additional context in comments.

1 Comment

This does exactly what I need in a way that is perfect for my implementation. Thank you.
1

If you want to partition, just return False if the string matches:

>>> sorted(list_of_lists, key=lambda value: value[0] != 'test_ss')
[('test_ss', 'Test 1'),
 ('test_ss', 'Test 4'),
 ('test_2_ss', 'Test 2'),
 ('test_3_ss', 'Test 3')]

7 Comments

that may turn out to be a good wild guess on what the OP wants
@Pynchia wild guess!
OK, maybe I am the only one who hasn't understood the question. I thought he wanted to sort by a specific position of the tuple, but it's unclear (see above my comment to the question)
@PeterWood - I think this is actually it. Let me pay around with it.
@PeterWood upvoted. I think you'd nailed it from the beginning
|
0

this will do

tuples = [
   ('test_ss', 'Test 1'),
   ('test_2_ss', 'Test 2'),
   ('test_3_ss', 'Test 3'),
   ('test_ss', 'Test 4')
]

sorted(tuples, key=lambda x: x[0].replace('test_ss','test_ _ss'))

[('test_ss', 'Test 1'),
 ('test_ss', 'Test 4'),
 ('test_2_ss', 'Test 2'),
 ('test_3_ss', 'Test 3')]

you want to treat 'test_ss' semantically as the smallest element in sorting order but it's not in textual representation. key.replace(..) handles it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.