P
P
pcdesign2020-05-02 09:56:37
Python
pcdesign, 2020-05-02 09:56:37

How to sort the list with cities?

Here is the list:

['Краснодар', 'Сочи', 'Нижний', 'Новгород', 'Новосибирск', 'Красноярск', 'Уфа', 'Санкт-Петербург', 'Владимир', 'Москва']


What is the best way to move Moscow and St. Petersburg to 1st and 2nd place, and sort the rest in alphabetical order?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
aRegius, 2020-05-02
@pcdesign

As an option:

first_cities = ['Москва', 'Санкт-Петербург']
first_plus_sorted_cities = first_cities + sorted(city for city in all_cities if city not in first_cities)

D
dmshar, 2020-05-02
@dmshar

A strange question - you sort your list, then transfer the necessary cities to its beginning. Moreover, the operation is done once and the list is short.
If the list is very long - first delete the necessary cities, sort the rest, then add the discarded cities to its beginning.
What is the problem?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question