Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJassim Alhatem
20,883 PointsI can't really understand this code
So I was scrolling down in treehouse's forum. And I found this code, that I understand but there's that one part that I'm struggling with:
def combo(iterable_1,iterable_2):
list_of_tuples = []
for index, item in enumerate(iterable_2, iterable_1[0]):
list_of_tuples.append((index,item))
return list_of_tuples
The 3rd line where it says enumerate(iterable_2, iterable_1[0]) can someone explain why he put in the [0] next to iterable_1.
[MOD: changed ''' to ``` formatting -cf]
2 Answers
Dave StSomeWhere
19,870 PointsThe second arg for enumerate is the starting int for the count (with a default of zero).
Your example is setting the start value to iterable_1[0]
which must be an integer.
Does that help?
Jassim Alhatem
20,883 PointsThank you so much, one last question:
print('Lets enumerate starting at 57')
for counter, value in enumerate(iterable_variable, 57): # specify a specific counter to start
print('the value is -->', value)
print('The counter is --> ', counter)
does the placement matter?
for counter, value in enumerate(N1, N2):
print('the value is -->', N1)
print('The counter is --> ', N2)
is that right?
And legit bro, thank you so freaking much. I can go to sleep right now it's like 3 AM. And I'm going to skip today's lesson but I've learned something from you. cheers Dave!!!!!!!
Dave StSomeWhere
19,870 PointsThe placement does matter, the command is enumerate(iterable, start=0)
. The first parameter is required and must be the iterable. The second parameter is optional and must be an integer.
Here's the documentation:
Return an enumerate object. iterable must be a sequence, an iterator, or some other object which supports iteration. The next() method of the iterator returned by enumerate() returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over iterable.
In your example N1 represents the individual entries of the iterable and N2 is the starting count value with a default of zero.
for counter, value in enumerate(N1, N2):
print('the value is -->', N1) # just an individual index entry from N1
# The enumerate action
print('The counter is --> ', N2) # equal to N2 only the first time, then N2 += 1
Jassim Alhatem
20,883 PointsJassim Alhatem
20,883 PointsDave StSomeWhere,
I'm sorry for being inconvenience, But can you explain a little bit more.
Dave StSomeWhere
19,870 PointsDave StSomeWhere
19,870 PointsNo inconvenience
In the code you posted - iterable_1 is being passed in and I'm not sure why someone would want to assume it is an integer and use only the first value (index zero) of that iterable. It's been a while since I've done that challenge and I'm pretty sure that the code you are referencing is incorrect and will not produce the desired result for the challenge. So, I feel that the code you are looking at is misleading. Regardless - understanding how
enumerate()
works what I think you are asking - right?Does the below code and generated output answer your question: