Python sorted list string

How to Sort List of Strings in Python

To sort a list of strings in Python you can use the sort() method. This method will order the list of strings in place, meaning that it will modify the original list and you won’t need to create a new list. You can also use the sorted() function to sort a list of strings, this returns a new list after sorting.

There are a few ways to sort the list of strings in Python.

  • Sorting in alphabetical/reverse order: You can use the built-in sort() or sorted() functions to sort a list of strings in alphabetical or reverse alphabetical order.
  • Based on the length of the string character: You can use the key argument of the sort() or sorted() function to sort a list of strings based on the length of the strings.
  • Sorting the integer values in a list of strings: If all of the strings in the list can be cast to integers, you can sort the list of strings by the integer values.

1. Quick Examples of Sort List of Strings

If you are in a hurry, below are some quick examples of the Python sort list of strings.

 # Below are the quick examples # Example 1: Sort list of strings technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion'] technology.sort() # Example 2: Sort list by length of strings technology.sort(key = len) # Example 3: Sort string by integer value use key as int strings = ['12','34','5','26','76','18','63'] strings.sort(key = int) # Example 4: Sort string in reverse order technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion'] technology.sort(reverse = True) # Example 5: Using sorted() method sorted_list = sorted(technology) # Example 6: Sorted string by integer value use key = int strings = sorted(strings, key=int) # Example 7: Sorted list of strings in descending order technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion'] technology = sorted(technology, reverse=True) 

2. Python Sort List of Strings

The sort() function is used to sort the list of strings in ascending order in python. This function modifies the original list in place and does not return a new list. For example, using the sort() function without any parameters to sort a list of strings in ascending order.

 # Use sort() method technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion'] technology.sort() print(technology) # Output # ['Hadoop', 'Hyperion', 'Java', 'NumPy', 'Pandas', 'Pyspark', 'Spark'] 

3. Sort List of Strings by sorted() Method

Alternatively, you can use the python sorted() built-in function to order the technology list of stings. It returns a new list containing all the elements from the technology list in sorted order.

 # Using sorted() method technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion'] sorted_list = sorted(technology) print(sorted_list) # Output: # ['Hadoop', 'Hyperion', 'Java', 'NumPy', 'Pandas', 'Pyspark', 'Spark'] 

4. Sort List by Length of Strings

To sort a list of strings based on the length of the strings, you can use the len() function as the key for the sort() function. For example, the list is sorted based on the length of the strings, with the shortest string coming first and the longest string coming last.

 # Sort list by length of strings technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion'] technology.sort(key = len) print(technology) # Output # ['Java', 'Spark', 'NumPy', 'Hadoop', 'Pandas', 'Pyspark', 'Hyperion'] 

5. Sort Strings using a Function

Similarly, If you want to sort a list of strings based on the integer value of the strings, you can convert the strings to integers by using int() function as the key for the sort() method or sorted() function. This sorts the list by numbers.

 # Sort string by integer value use key as int strings = ['12','34','5','26','76','18','63'] strings.sort(key = int) print(strings) # Sorted string by integer value use key = int strings = sorted(strings, key=int) print(strings) # Output: # ['5', '12', '18', '26', '34', '63', '76'] 

6. Sort Strings in Descending Order

To sort a list of strings in descending or reverse order, you can pass the reverse=True argument to the sort() method or sorted() function. Descending order is the opposite of ascending order where elements are arranged from highest to lowest value (for string Z to A).

 # Sort in descending order technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion'] technology.sort(reverse = True) print(technology) # Sorted list of strings in descending order technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion'] technology = sorted(technology, reverse=True) print(technology) # Output # ['Spark', 'Pyspark', 'Pandas', 'NumPy', 'Java', 'Hyperion', 'Hadoop'] 

Conclusion

In this article, I have explained how to sort the list of strings in python, first, I have covered using list.sort() function and python built-in function sorted().

References

You may also like reading:

Источник

Читайте также:  Sign Up

Python : How to Sort a list of strings ? | list.sort() Tutorial & Examples

list provides a member function sort(). It Sorts the elements of list in low to high order i.e. if list is of numbers then by default they will be sorted in increasing order. Whereas, if list is of strings then, it will sort them in alphabetical order.

Suppose we have a list of strings i.e.

#List Of Strings listOfStrings = ['hi' , 'hello', 'at', 'this', 'there', 'from']

Let’s sort this list of strings in different way i.e.

Frequently Asked:

Sort a List of strings in Alphabetical Order

''' Sort List of string alphabetically ''' listOfStrings.sort()

It will sort the list in alphabetically i.e.

['at', 'from', 'hello', 'hi', 'there', 'this']

Sort a List of strings alphabetically in Reverse Order

list.sort() accepts an another argument reverse. By default its value is False, but if its set to True then it will sort the list in reverse order.

So, contents of list will be now,

['this', 'there', 'hi', 'hello', 'from', 'at']

Sort a List of string by Length

list.sort( key=function )

list.sort() accepts an another argument key i.e. key Function. While sorting a list, all the elements of list will be compared with each other. Before comparison it will call the key function on each entry, to determine what should be compared.

To Sort a list of strings by length, provide len() as key function in sort i.e.

''' Sort List of string by Length by using len() as custom key function ''' listOfStrings.sort(key=len)

Now list contents will be,

['hi', 'at', 'this', 'from', 'there', 'hello']

Sort a List of string by Numeric Order

Suppose w e have a list of strings that contains numbers i.e.

listOfNum = ['55' , '101', '152', '98', '233', '40', '67']

To Sort a this list of strings by Numeric Order, provide int() as key function in sort i.e.

''' Sort in Ascending numeric order, pass key function that should convert string to integer i.e using int() ''' listOfNum.sort(key=int)

Now list contents will be,

Читайте также:  Mapstruct java map list

Sorting a list of strings by Numerically in descending Order

To Sort in Descending numeric order, pass reverse flag along with key function i.e.

''' Sort in Descending numeric order, pass reverse flag along with key function ''' listOfNum.sort(reverse=True, key=int)

Now list contents will be,

Complete example is as follows,

def main(): #List Of Strings listOfStrings = ['hi' , 'hello', 'at', 'this', 'there', 'from'] print(listOfStrings) ''' Sort List of string alphabetically ''' listOfStrings.sort() # Print the list print(listOfStrings) ''' Sort List of string alphabetically in Reverse Order ''' listOfStrings.sort(reverse=True) print(listOfStrings) ''' Sort List of string by Length by using len() as custom key function ''' listOfStrings.sort(key=len) print(listOfStrings) ''' Sort List of string by Numeric Order ''' listOfNum = ['55' , '101', '152', '98', '233', '40', '67'] # It will sort in alphabetical order listOfNum.sort() print(listOfNum) ''' Sort in Ascending numeric order, pass key function that should convert string to integer i.e using int() ''' listOfNum.sort(key=int) print(listOfNum) ''' Sort in Descending numeric order, pass reverse flag along with key function ''' listOfNum.sort(reverse=True, key=int) print(listOfNum) if __name__ == '__main__': main()
['hi', 'hello', 'at', 'this', 'there', 'from'] ['at', 'from', 'hello', 'hi', 'there', 'this'] ['this', 'there', 'hi', 'hello', 'from', 'at'] ['hi', 'at', 'this', 'from', 'there', 'hello'] ['101', '152', '233', '40', '55', '67', '98'] ['40', '55', '67', '98', '101', '152', '233'] ['233', '152', '101', '98', '67', '55', '40']

Источник

How to sort a list of strings?

This modifies your original list (i.e. sorts in-place). To get a sorted copy of the list, without changing the original, use the sorted() function:

for x in sorted(mylist): print x 

However, the examples above are a bit naive, because they don’t take locale into account, and perform a case-sensitive sorting. You can take advantage of the optional parameter key to specify custom sorting order (the alternative, using cmp , is a deprecated solution, as it has to be evaluated multiple times — key is only computed once per element).

Читайте также:  Mapstruct java map list

So, to sort according to the current locale, taking language-specific rules into account ( cmp_to_key is a helper function from functools):

sorted(mylist, key=cmp_to_key(locale.strcoll)) 

And finally, if you need, you can specify a custom locale for sorting:

import locale locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') # vary depending on your lang/locale assert sorted((u'Ab', u'ad', u'aa'), key=cmp_to_key(locale.strcoll)) == [u'aa', u'Ab', u'ad'] 

Last note: you will see examples of case-insensitive sorting which use the lower() method — those are incorrect, because they work only for the ASCII subset of characters. Those two are wrong for any non-English data:

# this is incorrect! mylist.sort(key=lambda x: x.lower()) # alternative notation, a bit faster, but still wrong mylist.sort(key=str.lower) 

Источник

Оцените статью