How to sort the list in the correct order?

0 like 0 dislike
3 views
You need to in the beginning was the values in the specified order, not the specified values are sorted as usual.

lst = ['asd', 'aaa', 'str1', 'str2', 'str3'] order = ['str1', 'str3'] result = ['str1', 'str3', 'aaa', 'asd', 'str2']
by | 3 views

1 Answer

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

Related questions

0 like 0 dislike
1 answer
asked Aug 6, 2019 by Leniorko
0 like 0 dislike
1 answer
asked May 22, 2019 by hatepls
0 like 0 dislike
1 answer
0 like 0 dislike
3 answers
asked May 22, 2019 by AlmazKayum
0 like 0 dislike
1 answer
asked May 3, 2019 by Program_Danil
110,608 questions
257,186 answers
0 comments
33,703 users