Conversion of Integer String to list in Python


The integer string is any number that sets within the double quote whereas the integer list is defined as the list of numbers separated by commas. In Python, we have some built−in functions− len(), isdigit(), append(), map(), fromstring(), and, split() that will be used for the Conversion of Integer String to list.

For example

The given integer string,

my_str = “1 2 3 4 5”

The final output become [1, 2, 3, 4, 5]

Syntax

The following syntax is used in the examples-

len()

The len() is a built−in method in Python that returns the length of the object.

isdigit()

This isdigit() is an in−built function in Python that returns true if all the characters satisfy as a digit, otherwise returns false.

append()

The append() is a built−in function in Python that adds the element at the end of the list.

map()

The built−in function map() allows a specific function for each element iteration.

fromstring()

The fromstring() follows the module named numpy to create the one-dimensional array in which the array string contains data.

split()

The split() is a built−in method in Python that separate the string in the form of a list.

Using while loop

In the following example, we will use the while loop which uses some built−in function and operator to iterate through the input string based on specific conditions and use the += operator to separate the integer string by comma(,) to get the integer list.

Example

str_int = '14 15 17 18'
lst_int = []
i = 0
while i < len(str_int):
    if str_int[i].isdigit():
        num = ''
        while i < len(str_int) and (str_int[i].isdigit() or str_int[i] == '-'):
            num += str_int[i]
            i += 1
        lst_int.append(int(num))
    else:
        i += 1
print("Conversion of integer string into integer list:", lst_int)

Output

 Conversion of integer list into list: [14, 15, 17, 18]

Using map() and split() Function

In the following example, start the program by initializing the input string. Then use some of the built−in functions such as list(), map(), and, split() to calculate the conversion of integer string into integer list and get the result.

Example

int_str = "1 2 3 4 5"
res = list(map(int, int_str.split()))
print("Conversion of integer string into integer list:", res)

Output

Conversion of integer string into integer list: [1, 2, 3, 4, 5]

Using list comprehension

In the following example, start the program by storing the integer string in the variable int_str. Then use the list comprehension where variable i iterate through the given input string i.e. int_str by using for loop and storing it in the variable lst_int. Display the result.

Example

int_str = '4573'
lst_int = [int(i) for i in int_str]
print("Conversion of integer string into integer list:", lst_int)

Output

 Conversion of integer string into integer list: [4, 5, 7, 3]

Using numpy Module

In the following example, we will start the program by importing the module named numpy. Then store the integer string in the variable str_int. Next, use the built−in function fromstring() that accepts three parameters− str_int(given input), dtype(specifies the value as an integer by using keyword int), sep(separating number in data), and all these processes store it in the variable res_list. Finally, we are printing the result with the help of a variable named res_list.

Example

import numpy as np
str_int = '11 16 17 14 89'
res_list = np.fromstring(str_int, dtype=int, sep=' ').tolist()
print("Conversion of integer string into integer list:", res_list)

Output

 Conversion of integer string into integer list: [11, 16, 17, 14, 89]

Using for loop

In the following example, the program uses for loop to iterate through the input string with the help of built−in function split(). Using append() it will add the element at the end of list. Finally, display the result.

Example

str_int = '20 40 61 82 10'
lst_integers = []
for item in str_int.split(' '):
    lst_integers.append(int(item))
print("Conversion of integer string into integer list:", lst_integers)

Output

 Conversion of integer string into integer list: [20, 40, 61, 82, 10]

Conclusion

We discussed the various ways to solve the Conversion of an Integer String into an Integer list. This is an important operation that uses various built−in functions such as isdigit(), append(), etc. to perform specific tasks. This program is commonly used in some applications such as Algorithmic problem−solving, Input validation, and, Data preprocessing.

Updated on: 14-Aug-2023

109 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements