TypeError: list indices must be integers, not str Python [duplicate]
Asked Answered
O

5

12

list[s] is a string. Why doesn't this work?

The following error appears:

TypeError: list indices must be integers, not str

list = ['abc', 'def']
map_list = []

for s in list:
  t = (list[s], 1)
  map_list.append(t)
Overcurious answered 27/12, 2014 at 13:8 Comment(1)
please don't use list as a name, it's a shadow of python built-inDuple
S
2
list1 = ['abc', 'def']
list2=[]
for t in list1:
    for h in t:
        list2.append(h)
map_list = []        
for x,y in enumerate(list2):
    map_list.append(x)
print (map_list)

Output:

>>> 
[0, 1, 2, 3, 4, 5]
>>> 

This is what you want exactly.

If you dont want to reach each element then:

list1 = ['abc', 'def']
map_list=[]
for x,y in enumerate(list1):
    map_list.append(x)
print (map_list)

Output:

>>> 
[0, 1]
>>> 
Shien answered 27/12, 2014 at 13:15 Comment(1)
I downvoted because this does not explain why the original code did not work or where the error in the OP's understanding was.Deprecatory
M
14

When you iterate over a list, the loop variable receives the actual list elements, not their indices. Thus, in your example s is a string (first abc, then def).

It looks like what you're trying to do is essentially this:

orig_list = ['abc', 'def']
map_list = [(el, 1) for el in orig_list]

This is using a Python construct called list comprehension.

Motorman answered 27/12, 2014 at 13:10 Comment(0)
B
6

Do not use the name list for a list. I have used mylist below.

for s in mylist:
    t = (mylist[s], 1)

for s in mylist: assigns elements of mylist to s i.e s takes the value 'abc' in the first iteration and 'def' in the second iteration. Thus, s can't be used as an index in mylist[s].

Instead, simply do:

for s in lists:
    t = (s, 1)
    map_list.append(t)
print map_list
#[('abc', 1), ('def', 1)]
Bron answered 27/12, 2014 at 13:10 Comment(0)
I
2

it should be:

for s in my_list:     # here s is element  of list not index of list
    t = (s, 1)
    map_list.append(t)

i think you want:

for i,s in enumerate(my_list):  # here i is the index and s is the respective element
    t = (s, i)
    map_list.append(t)

enumerate give index and element

Note: using list as variable name is bad practice. its built in function

Introversion answered 27/12, 2014 at 13:10 Comment(0)
S
2
list1 = ['abc', 'def']
list2=[]
for t in list1:
    for h in t:
        list2.append(h)
map_list = []        
for x,y in enumerate(list2):
    map_list.append(x)
print (map_list)

Output:

>>> 
[0, 1, 2, 3, 4, 5]
>>> 

This is what you want exactly.

If you dont want to reach each element then:

list1 = ['abc', 'def']
map_list=[]
for x,y in enumerate(list1):
    map_list.append(x)
print (map_list)

Output:

>>> 
[0, 1]
>>> 
Shien answered 27/12, 2014 at 13:15 Comment(1)
I downvoted because this does not explain why the original code did not work or where the error in the OP's understanding was.Deprecatory
E
0

for s in list will produce the items of the list and not their indexes. So s will be 'abc' for the first loop, and then 'def'. 'abc' could only be a key to a dict, not a list index.

In the line with t fetching the item by index is redundant in python.

Earthlight answered 27/12, 2014 at 13:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.