Convert String List To Int List Python
Last Updated : Jul 1, 2023
IN - Python
In this article we will show you the solution of convert string list to int list python, Int, character, and float are just a few of the many object types that can be included in a Python list.
A list is nothing more than an ordered series. An array in Python is the same of a list in other computer languages, and vice versa.
Two entries in the list are separated by a comma (,), which is displayed in square brackets.
A list in Python can simultaneously hold many data types, making either one homogeneous or heterogeneous.
In contrast, an array in a number of different programming languages can only contain a single type of data, keeping it homogeneous in nature.
Step By Step Guide On Convert String List To Int List Python :-
Python libraries make it simple to convert between different data formats.
Yet in the development field, the issue of converting the comprehensive list containing strings to numbers is pretty common.
Method - Using list comprehension
test_list = ['1', '3', '8', '11', '13'] test_list = [int(i) for i in test_list] print ("Modified list is : " + str(test_list))
- Here, you can see how to create a Python programme that uses the list comprehension technique to transform a string list to an int list.
- The list is initialised in the first code line. After that, the conversion is carried out using list comprehension.
- A type of shortcut that developers always search for is list comprehension. It avoids the complexity and effort involved in writing code for a solution.
- Next, using the print function, we display that changed list.
Method - Using the ast.literal_eval() function from the ast module
import ast test_list = ['1', '4', '3', '6', '7'] test_list = [ast.literal_eval(s) for s in test_list] print("Modified list:", test_list)
- As you can see, we used the ast.literal eval() function from ast module to create a Python programme that converts a string list to an int list.
- We import that Python module known as ast in the first code line.
- Python programmes can handle trees of a Python abstract syntax grammatical with the help of the ast module.
- This module aids in programmatically determining the current grammar in use as the generic syntax itself could vary with each Python release.
- After initialising the list of strings, we use the ast.literal eval() method from the ast module to transform the list of strings into a list of numbers.
- With the help of this function, you may evaluate a string like a Python literal, which enables you to parse & evaluate strings which contain Python expressions, including numbers, lists, dictionaries, and other data types.
- We use the print function to print that changed list in the final line of code.
Thus, we were able to understand the concept of Python string list into int list conversion.
Also, we learnt how to transform a string list to an integer list in Python using the list comprehension technique and how to use the ast.literal eval() function from ast package.
I hope this article on convert string list to int list python helps you and the steps and method mentioned above are easy to follow and implement.