-1

Apologies if this question has already been covered!I am trying to convert each element in this list of listsinto integer format. I have used two for loops to iterate through the list, in the same way we do it "C" There is an error that says " Object is not subscriptable" in Line 3 . Am i missing something obvious?

Here is the code:

l=[['1','2'],['3','4']]

for i in range(len(l)):
    for j in range(len[i]):
        l[j]=int(l[j])
Nils Werner
  • 31,964
  • 7
  • 67
  • 91
agastya teja
  • 279
  • 1
  • 4
  • 17
  • 1
    You are using square brackets on len which is a function. Besides, the recommended way of doing this conversion is a list comprehension: `[[int(k) for k in sl] for sl in l]`. – Paul Panzer Feb 25 '17 at 10:48

1 Answers1

-1

You can use nested list comprehensions:

outer = [['1','2'],['3','4']]
outer_as_ints = [[int(x) for x in inner] for inner in outer]
Nils Werner
  • 31,964
  • 7
  • 67
  • 91