
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Convert list of string into sorted list of integer in Python
Analysing data using python can bring us scenario when we have to deal with numbers represented as strings. In this article we will take a list which has numbers present as strings and we need to convert then to integers and then represent them in a sorted manner.
With map and sorted
In this approach we apply the int function to every element of the list using map. Then we apply the sorted function to the list which sorts the numbers. It can handle negative numbers also.
Example
listA = ['54', '21', '-10', '92', '5'] # Given lists print("Given list : \n", listA) # Use mapp listint = map(int, listA) # Apply sort res = sorted(listint) # Result print("Sorted list of integers: \n",res)
Output
Running the above code gives us the following result −
Given list : ['54', '21', '-10', '92', '5'] Sorted list of integers: [-10, 5, 21, 54, 92]
With int and sort
In this approach we apply the int function by using a for loop and store the result into a list. Then the sort function is applied to the list. The final result shows the sorted list.
Example
listA = ['54', '21', '-10', '92', '5'] # Given lists print("Given list : \n", listA) # Convert to int res = [int(x) for x in listA] # Apply sort res.sort() # Result print("Sorted list of integers: \n",res)
Output
Running the above code gives us the following result −
Given list : ['54', '21', '-10', '92', '5'] Sorted list of integers: [-10, 5, 21, 54, 92]
With sorted and int
This approach is similar to above except that we apply int function through a for loop and enclose the result in the sorted function. It is a single expression which gives us the final result.
Example
listA = ['54', '21', '-10', '92', '5'] # Given lists print("Given list : \n", listA) # Convert to int res = sorted(int(x) for x in listA) # Result print("Sorted list of integers: \n",res)
Output
Running the above code gives us the following result −
Given list : ['54', '21', '-10', '92', '5'] Sorted list of integers: [-10, 5, 21, 54, 92]
- Related Articles
- Convert a string representation of list into list in Python
- Convert list of tuples into list in Python
- Convert list into list of lists in Python
- Program to convert List of Integer to List of String in Java
- Program to convert List of String to List of Integer in Java
- Convert list of string to list of list in Python
- Python - Convert list of string to list of list
- Convert a list of multiple integers into a single integer in Python
- Convert list of numerical string to list of Integers in Python
- Convert list of tuples into digits in Python
- Python - Convert given list into nested list
- How can we convert a list of characters into a string in Python?
- Convert string enclosed list to list in Python
- Convert a list into tuple of lists in Python
- Convert list of tuples to list of list in Python
