0

How can I convert the two other elements in the list from str to int?

a = [1, "2", "3"] print(type(a[0])) print(type(a[1])) print(type(a[2])) output:<class 'int'> <class "str"> <class "str">

27th Oct 2022, 3:36 PM
Shantanu
Shantanu - avatar
2 Respuestas
+ 3
int( a[1] ) int( a[2] ) edit: Shantanu by map : a = [1, "2", "3"] a = list( map(int,a) ) print(a)
27th Oct 2022, 3:42 PM
Jayakrishna 🇮🇳
+ 7
Shantanu , you can do this by using a for loop, so that the replacement can be done in-place. to replace the current *str number* by an *int number*, we need the index of each list item. this can be achieved by using enumerate() inside the for loop: ... for ind, num in enumerate(yourlist): ... then use the list name and the index to assign the str numer (has to be converted to int) to the list item
27th Oct 2022, 3:47 PM
Lothar
Lothar - avatar