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 trial

Python Python Sequences Sequence Iteration Iterating With For Loops

Edit the code so that the for loop uses the enumerate method. Add a print statement above the existing print statement t

Python Sequences Challenge Task 2 of 2

iterating_lists.py
rainbow = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']
for current_element in rainbow:
    print(current_element)
for index, current_element in enumerate(rainbow, 1):
    print(f'{index}. {current_element}')

5 Answers

Josh Keenan
Josh Keenan
20,315 Points

The challenge wants you to replace the first for loop for the one with enumerate and then add another print statement to output the item:

for index, current_element in enumerate(rainbow):
    print(index)
    print(current_element)
Immanuel wiessler
Immanuel wiessler
2,726 Points

Guys, I found the answer the way to solve the second question is pretty easy, here is the solution

rainbow = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'] 
     for index, current_element in enumerate(rainbow, 0):
           print(f'{index}. {current_element}') 

you just need to change the enumerate starting value to 0 instead of 1

Steven Parker
Steven Parker
231,268 Points

That gets the result, but it's not done the way the instructions asked for.

David Dunlop
David Dunlop
2,844 Points

works like a champ Immanuel thanks

This can work also.

rainbow = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']
for index,color in enumerate(rainbow):
    print(index)
    print(color)
Steven Parker
Steven Parker
231,268 Points

The instructions say "Edit the code so that the for loop uses the enumerate method." So unlike most challenges, you will change the task 1 code instead of adding another loop.

The rest of the instructions say "Add a print statement above the existing print statement that prints the index of the current element." So the new print should print only the index. The other one will still print the value.

brandon supinski
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
brandon supinski
Python Development Techdegree Graduate 27,983 Points

This is how I got it to pass not sure if its intended or not Steven Parker can verify.

rainbow = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'] for item in enumerate(rainbow): print(f'index') print(item)