L
L
lololololo2018-04-29 23:56:32
Python
lololololo, 2018-04-29 23:56:32

How to sort the list in the right order?

You want the values ​​to start in the specified order, and not the specified values ​​to be sorted as usual.

lst = ['asd', 'aaa', 'str1', 'str2', 'str3']
order = ['str1', 'str3']

result = ['str1', 'str3', 'aaa', 'asd', 'str2']

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Codebaker, 2018-04-30
@Codebaker

sublist = [item for item in lst if item not in order]
sublist.sort()
result = order + sublist

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question