Built in function Grammar/Environments List

[Python] How to Convert strings in list to either int or float

This article shows how to convert list of string to either int or float.

Contents

  • convert 1D list using "for statement"
  • convert 1D list using "map( ) function"
  • convert 2D list using "for statement"
  • convert 2D list using "map( ) function" and "comprehension"

sponsored link

convert 1D list using "for statement"

In the beginning, I show how to convert 1D list using "for statement".

Data type conversion is described as follows.

How to write

convert to int : int(value)

convert to float : float(value)

convert to string : str(value)

This is an example code.

Using "for statement", strings in 1D list are converted to int type.

list_1D = ['11', '12', '13', '14']

list_1D_int = []
for data in list_1D :
    list_1D_int.append(int(data))

print(list(list_1D_int))
# [11, 12, 13, 14]

> for data in list_1D :

The elements are taken out of the list one by one.

> list_1D_int.append(int(data))

The elements converted to int type are added to the list "list_1D_int".

This is an example code to convert to float type.

list_1D = ['1.1', '1.2', '1.3', '1.4']

list_1D_float = []
for data in list_1D :
    list_1D_float.append(float(data))

print(list(list_1D_float))
# [1.1, 1.2, 1.3, 1.4]

> list_1D_float.append(float(data))

The elements converted to float type are added to the list "list_1D_float".

As I show in the next section, the map( ) function can be used for a more concise description

sponsored link

convert 1D list using "map( ) function"

I show how to convert 1D list using "map( ) function".

By using map(), function can be applied to all elements of the iterable object( list, tuple, etc.).

This function can be a built-in function, a user-defined function, or a lambda function.

How to write

map( Function, iterable object )

This is an example code to convert to int type.

list_1D = ['11', '12', '13', '14']

list_1D_int = map(int, list_1D)

print(list_1D_int)
# <map object at 0x0000026AB7447C88>
print(list(list_1D_int))
# [11, 12, 13, 14]

The map( ) function returns a map object.

The elements cannot be checked by print( ).

Therefore, list( ) function is used to convert the map object to the list.

How to use of map( ) function is explained in this article.

How to use of map( ) function

This is an example code to convert to float type.

list_1D = ['1.1', '1.2', '1.3', '1.4']

list_1D_float = map(float, list_1D)

print(list_1D_float)
# <map object at 0x0000026AB7447C88>
print(list(list_1D_float))
# [1.1, 1.2, 1.3, 1.4]

> list_1D_float = map(float, list_1D)

By using float( ) function, strings in list are converted to float type.

sponsored link

convert 2D list using "for statement"

I show how to convert 2D list using "for statement".

This is an example code to convert to int type.

list_2D = [
    ['11', '12', '13', '14'],
    ['21', '22', '23', '24'],
    ['31', '32', '33', '34']
]

list_2D_int = []

for row in list_2D:
    row_int = []
    for data in row:
        row_int.append(int(data))
    list_2D_int.append(row_int)

print(list_2D_int)
# [[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34]]
print(type(list_2D_int[0][0]))
# <class 'int'>

> for row in list_2D :

The elements are taken out of the list line by line.

> for data in row:

The elements are taken one by one from the row data.

> row_int.append(int(data))

The elements converted to int type are added to the list "row_int".

> list_2D_int.append(row_int)

The list "row_int", which contains one row of elements, is added to "list_2D_str".

sponsored link

convert 2D list using "map( ) function" and "comprehension"

I show how to convert 2D list using "map( ) function" and "comprehension".

This is an example code to convert to int type.

list_2D = [
    ['11', '12', '13', '14'],
    ['21', '22', '23', '24'],
    ['31', '32', '33', '34']
]

list_2D_int = [list(map(int, row)) for row in list_2D]

print(list_2D_int)
# [[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34]]

> list_2D_int = [list(map(int, row)) for row in list_2D]

The elements are taken from the list "list_2D" line by line, passed to map( ) function.

How to use of comprehension is explained in this article.

How to use of comprehension

sponsored link

-Built in function, Grammar/Environments, List
-, ,