Search code examples
pythonindexingenumerate

Access "Actions" object in a list using enumerate() in python


# Python program to illustrate 
# enumerate function 
l1 = [{'Actions':("eat","sleep","repeat"), 'members':("1028", "jeram", "chilaw")}] 
s1 = "geek"

# creating enumerate objects 
obj1 = enumerate(l1) 
obj2 = enumerate(s1) 

print "Return type:",type(obj1) 
print list(enumerate(l1['Actions'])) 

# changing start index to 2 from 0 
print list(enumerate(s1,2)) 

need to print "Action" object in the (l)1 list using enumerate function. but occur an error which said list indices must be integers, not str. help me to solve this.

error i got is,

Traceback (most recent call last):
  File "/home/25b7a4de08d5472b64b462006452cf1f.py", line 11, in <module>
    print list(enumerate(l1['Actions'])) 
TypeError: list indices must be integers, not str

Give me any solution please.Thanks in advance.


Solution

  • You are not accessing the dictionary; index your list, get the dictionary.

    Use:

    list(enumerate(l1[0]['Actions']))
    

    instead of:

    list(enumerate(l1['Actions']))