How to Sort a List Alphabetically

Python data structure sort list alphabetically

[] 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.

How to sort a list alphabetically?

Here's a method that do not need to unlist(). After making the list with random letters, assign names to the list with the content of the list. Then order the names within the list.

Input

random_text_data = sample(letters, 10)

[1] "c" "v" "m" "g" "h" "l" "d" "i" "u" "y"

Original solution

list_text_data = as.list(random_text_data)

list_text_data <- setNames(list_text_data, list_text_data)

list_text_data[order(names(list_text_data))]

$c
[1] "c"

$d
[1] "d"

$g
[1] "g"

$h
[1] "h"

$i
[1] "i"

$l
[1] "l"

$m
[1] "m"

$u
[1] "u"

$v
[1] "v"

$y
[1] "y"

Updated solution

Inspired by @zx8754's answer, you don't actually need to change names of the list, you can use setNames within order, then the output will be an unnamed list.

list_text_data[order(names(setNames(list_text_data, list_text_data)))]

[[1]]
[1] "c"

[[2]]
[1] "d"

[[3]]
[1] "g"

[[4]]
[1] "h"

[[5]]
[1] "i"

[[6]]
[1] "l"

[[7]]
[1] "m"

[[8]]
[1] "u"

[[9]]
[1] "v"

[[10]]
[1] "y"

How to sort multiple strings inside list alphabetically ascending in Python

For each string, you can use str.split to get individual words + sorted to sort the words + join to join back the words into a single string in a list comprehension:

[' '.join(sorted(x.split())) for x in lst]

Output:

['apple banana', 'cat dog elephant']

Sort list of strings alphabetically

You need to sort your elements based lowercase representation of the strings:

sorted(['Pera','mela','arancia','UVA'], key=str.lower)

this will output:

['arancia', 'mela', 'Pera', 'UVA']

Sort List by alphabetical order

< and > is usually a shortcut to a compareTo method.

just use that method instead.

data.sort((a, b) {
return a['name'].toLowerCase().compareTo(b['name'].toLowerCase());
});

How to sort a list by length of string followed by alphabetical order?

You can do it in two steps like this:

the_list.sort() # sorts normally by alphabetical order
the_list.sort(key=len, reverse=True) # sorts by descending length

Python's sort is stable, which means that sorting the list by length leaves the elements in alphabetical order when the length is equal.

You can also do it like this:

the_list.sort(key=lambda item: (-len(item), item))

Generally you never need cmp, it was even removed in Python3. key is much easier to use.

How to sort tuples in a list alphabetically if they have the same value, without changing the descending value?

You can try this, assuming s is your list.. (list keyword is a saved keyword and not recommended to use as variable...):

s = sorted(s, key = lambda x: (-x[1], x[0]))

Note that your specific example can be just sorted by the first element, but to be more generic you can just use this sort for cases where it wont be enough for your expected result



Related Topics



Leave a reply



Submit