2

I want to cast elements in a list (string to integer) Can't find what my mistake is. I just get strings. Some other post answers suggest list comprehensions, but, being a newbie, I prefer understanding why this more basic approach doesn't work, before learning list comprehensions.

Thanks for your help.

(Using Python 3)

I tried:

while True:
    userInput=input("Write space-separated numbers:  ")
    listNumbers=userInput.split()
    for i in listNumbers:
        int(i)
    print(type(listNumbers[0]))

Also tried:

for i in listNumbers:
    i=int(i)

I expect the type(listNumbers[0]) or whatever index number to return integer but the output is still a string.

lljr
  • 23
  • 5

5 Answers5

2

You must do:

for i in range(0, len(listNumbers)):
    listNumbers[i] = int(listNumbers[i])

or:

for idx, val in enumerate(listNumbers):
    listNumbers[idx] = value

This can be shortened using:

listNumbers = [int(x) for x in listNumbers]

When doing:

for i in listNumbers):
    i = int(i)

i is only a reference to the elements so you can't change the original value of the list.

When doing:

for i in listNumbers:
    int(i)

You don't store the result of int(i).

DSC
  • 1,153
  • 7
  • 21
  • I understood that I did nothing with the int(i) alone, but with i=int(i), it's more difficult to understand to me, because it seems it's the natural way of casting and keeping the int property like memorized in the i variable. I'll work with the elements index, as proposed. Bedankt! – lljr Jun 09 '19 at 11:20
1

The problem comes from the fact that you do not store the integer in the loop !

Do :

mylist = []
for i in listNumbers:
    mylist.append(int(i))

The best solution is

listNumbers = list(map(int, listNumbers))
dallonsi
  • 1,299
  • 1
  • 8
  • 29
  • Liked that, because probably it's the shortest way, and still using the for loop. Merci! – lljr Jun 09 '19 at 11:24
1

int(i) does not convert the element i in the list to an integer, you would need to explicitly assign int(i) back to the element in the list for that to happen

while True:

    userInput=input("Write space-separated numbers:  ")
    listNumbers=userInput.split()

    #Iterate over the list and get index and element of list
    for idx, i in enumerate(listNumbers):
        #Cast string to int and assign back to list
        listNumbers[idx] = int(i)

    #Get the type
    print(type(listNumbers[0]))

The output will be

Write space-separated numbers:  1 3 5
<class 'int'>
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
1

you need to actually save your modified value back into the list

for i,val in enumerate(my_list):
    my_list[i] = int(val)

but as mentioned its much better to just use a list comprehension

new_list = [int(val) for val in my_list]
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
1

Your first variant doesn't do anything, because the cast value is just thrown away.

Your second variant does assign the value to i, but that doesn't change the value that is stored in the list.

I suggest using a list comprehension instead:

while True:
    userInput=input("Write space-separated numbers:  ")
    listNumbers=[int(i) for i in userInput.split()]
    print(type(listNumbers[0]))
L3viathan
  • 26,748
  • 2
  • 58
  • 81