Python data structure sort list alphabetically
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT
Music by Eric Matyas
https://www.soundimage.org
Track title: Techno Intrigue Looping
--
Chapters
00:00 Python Data Structure Sort List Alphabetically
00:26 Answer 1 Score 18
00:37 Answer 2 Score 11
00:46 Accepted Answer Score 329
02:02 Answer 4 Score 44
02:51 Thank you
--
Full question
https://stackoverflow.com/questions/1403...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #list #sorting #alphabetical
#avk47
ACCEPTED ANSWER
Score 329
[] denotes a list, () denotes a tuple and {} denotes a dictionary. You should take a look at the official Python tutorial as these are the very basics of programming in Python.
What you have is a list of strings. You can sort it like this:
In [1]: lst = ['Stem', 'constitute', 'Sedge', 'Eflux', 'Whim', 'Intrigue']
In [2]: sorted(lst)
Out[2]: ['Eflux', 'Intrigue', 'Sedge', 'Stem', 'Whim', 'constitute']
As you can see, words that start with an uppercase letter get preference over those starting with a lowercase letter. If you want to sort them independently, do this:
In [4]: sorted(lst, key=str.lower)
Out[4]: ['constitute', 'Eflux', 'Intrigue', 'Sedge', 'Stem', 'Whim']
You can also sort the list in reverse order by doing this:
In [12]: sorted(lst, reverse=True)
Out[12]: ['constitute', 'Whim', 'Stem', 'Sedge', 'Intrigue', 'Eflux']
In [13]: sorted(lst, key=str.lower, reverse=True)
Out[13]: ['Whim', 'Stem', 'Sedge', 'Intrigue', 'Eflux', 'constitute']
Please note: If you work with Python 3, then str is the correct data type for every string that contains human-readable text. However, if you still need to work with Python 2, then you might deal with unicode strings which have the data type unicode in Python 2, and not str. In such a case, if you have a list of unicode strings, you must write key=unicode.lower instead of key=str.lower.
ANSWER 2
Score 44
Python has a built-in function called sorted, which will give you a sorted list from any iterable you feed it (such as a list ([1,2,3]); a dict ({1:2,3:4}), although it will just return a sorted list of the keys; a set ({1,2,3,4); or a tuple ((1,2,3,4))).
>>> x = [3,2,1]
>>> sorted(x)
[1, 2, 3]
>>> x
[3, 2, 1]
Lists also have a sort method that will perform the sort in-place (x.sort() returns None but changes the x object) .
>>> x = [3,2,1]
>>> x.sort()
>>> x
[1, 2, 3]
Both also take a key argument, which should be a callable (function/lambda) you can use to change what to sort by.
For example, to get a list of (key,value)-pairs from a dict which is sorted by value you can use the following code:
>>> x = {3:2,2:1,1:5}
>>> sorted(x.items(), key=lambda kv: kv[1]) # Items returns a list of `(key,value)`-pairs
[(2, 1), (3, 2), (1, 5)]
ANSWER 3
Score 18
You're dealing with a python list, and sorting it is as easy as doing this.
my_list = ['Stem', 'constitute', 'Sedge', 'Eflux', 'Whim', 'Intrigue']
my_list.sort()
ANSWER 4
Score 11
You can use built-in sorted function.
print sorted(['Stem', 'constitute', 'Sedge', 'Eflux', 'Whim', 'Intrigue'])