1
L = [['1','2'], ['3,','4']]
for innerL in L:<br/>
    for item in innerL:
        item = int(item)

>>> print(L)
[['1','2'], ['3,','4']]

Why do the strings of numbers not change into integers? If I type:

>>> type(int('3'))
<class 'int'>

then shouldn't the item be turned into an integer? What am I missing?

SpaSmALX
  • 23
  • 3

2 Answers2

1

They are integers?

L = [['1','2'], ['3','4']]
for innerL in L:
  for item in innerL:
    item = int(item)
    print(item)
    print(isinstance(item, int))

Output:

1 True 2 True 3 True 4 True

0

The name used is only a reference; if you want to change the sequence then you need to change it directly.

innerL[:] = [int(e) for e in innerL]
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Doesn't item in the for loop actually refer to the specific piece of data inside the list? If you type x = '3' then x = int(x), x will refer to 3. I'm still slightly confused. – SpaSmALX Dec 13 '17 at 23:27
  • @SpaSmALX: Long story short: "reference" doesn't mean the same thing in Python that it does in PHP and C++. – Ignacio Vazquez-Abrams Dec 14 '17 at 02:54