All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Convert String List To Int List Python

Last Updated : Mar 11, 2024

Convert String List To Int List 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))
  1. 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.
  2. The list is initialised in the first code line. After that, the conversion is carried out using list comprehension.
  3. 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.
  4. 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)
  1. 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.
  2. We import that Python module known as ast in the first code line.
  3. Python programmes can handle trees of a Python abstract syntax grammatical with the help of the ast module.
  4. This module aids in programmatically determining the current grammar in use as the generic syntax itself could vary with each Python release.
  5. 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.
  6. 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.
  7. We use the print function to print that changed list in the final line of code.

Conclusion :-

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.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪