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

Stanley Nkosi
Stanley Nkosi
4,233 Points

Python Sequence -Task 1 of 2 How to write a loop that iterates over the list provided? Stanley Nkosi

How to write a loop that iterates over the list provided?

iterating_lists.py
rainbow = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']
for rainbow = colors
for index rainbow in enumerate (colors):
print(f'{'index'}. {rainbow}
Stanley Nkosi
Stanley Nkosi
4,233 Points

Thanks a lot Josh.Your solution was spot on.I can sleep with peace. Stanley Nkosi

Josh Keenan
Josh Keenan
19,652 Points

no problem, you can mark this as solved by selecting best answer, happy coding!

1 Answer

Josh Keenan
Josh Keenan
19,652 Points

Here's my solution:

rainbow = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']

for i in rainbow:
    print(i)

The for loop is what we are using to go over the items in the list. We are needing to output the index of the item in the list and the value i has this. Then we just need to print that out. Hope this makes sense and feel free to ask any questions

Stanley Nkosi
Stanley Nkosi
4,233 Points

Hi Josh, I did respond last night.All the same thanks a lot.I was battling and I did not know whether I am coming or going.Thanks Josh.