0

How to sort a list in alphabetical order in python

6th Aug 2020, 5:05 PM
Amit Kumar
Amit Kumar - avatar
6 ответов
+ 3
list.sort()
6th Aug 2020, 5:17 PM
Julia Shabanova
Julia Shabanova - avatar
+ 3
Granger a set is not an ordered list. Converting a list to a set will remove any duplicate values and return an unordered collection. https://docs.python.org/3.8/library/stdtypes.html#set-types-set-frozenset Amit Kumar You can use the list.sort() method as Julia Shabanova has stated to sort the original list itself in place in lexicographical order, or use the lst = sorted(list) function to return a copy of the lexicographically sorted list. Note that uppercase letters have a lower value and will be ordered prior to lowercase. https://docs.python.org/3/tutorial/datastructures.html https://docs.python.org/3/library/functions.html#sorted
6th Aug 2020, 6:15 PM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Granger You stated "Set is a sorted list.", this is false. That is all I was commenting on concerning your post, but you're welcome, I guess.
6th Aug 2020, 7:34 PM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Granger That is true, but the 2 statements do not correlate. Just because there are no duplicates in the original list does not mean that converting to a set (using set(list)) will give you a sorted list. In fact after using set() you won't have a list at all, but rather a set object which the order of its elements will be arbitrary. I'm simply trying to clarify for the OP, so as to give factual information that won't confuse them or anyone who reads your post any further, if they don't understand what a set is in the first place.
6th Aug 2020, 7:44 PM
ChaoticDawg
ChaoticDawg - avatar
0
Use the sort()method [whatever].sort ()
8th Aug 2020, 2:23 AM
...
... - avatar
- 1
Thank u
6th Aug 2020, 5:19 PM
Amit Kumar
Amit Kumar - avatar