Enumerate Functions


#1



https://www.codecademy.com/courses/python-beginner-en-cxMGf/2/2?curriculum_id=4f89dab3d788890003000096


My code is working fine, but I have a question about Python logic. Why do we have to list "index" in our for loop? Isn't enumerate basically just listing indices, and if so, doesn't index by virtue of that fact become a keyword? And if index is a keyword, doesn't that mean it is in basic Python language, thus not requiring us to list it in our for loop? Couldn't we just say: "for item in enumerate(choices): print index + 1, item" ?


choices = ['pizza', 'pasta', 'salad', 'nachos']

print 'Your choices are:'
for index, item in enumerate(choices):
    print index + 1, item


#2

lets look:

print list(enumerate(choices))

see how enumerate create tuples? (0, 'pizza') for example

so we need two variables, one to get the indexes from the tuples and a second to get the items/values from the tuple


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.