You are about to sort the list in alphabetical order

Asked 2 years ago, Updated 2 years ago, 71 views

You want to sort the list that stores the string in alphabetically. Where are the relevant methods?

list dictionary sorting python

2022-09-21 20:01

1 Answers

Usually

mylist = ["b", "C", "A"]
mylist.sort()

We use it together. Because this method changes the existing list (return None),

Use the sorted() function if you want to have a sorted list without changing the existing list, or if you want only the output in ascending order.

mylist = ["b", "C", "A"]
sorted_list = sorted(mylist)

for x in sorted(mylist):
    print x

Each country may have a different dictionary definition, so if you consider this, you should use the locale module to give an additional key to compare.

import locale
import functools

mylist = ["Apple", "Banana", "Strawberry", "Grape"]

locale.setlocale(locale.LC_ALL, '') #set by Korean standard
sortedByLocale = sorted(mylist, key=functools.cmp_to_key(locale.strcoll))


2022-09-21 20:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.