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: Shopping List - List and Strings

In challenge Task 3 of 4 it says "Almost done! Now, swap the value of "Treehouse" in greeting_list to be the first item in your name_list variable."

here is my code:

full_name = "Daniel Chikaka"

name_list = full_name.split()

greeting_list = "Hi, I'm Treehouse".split()

name_list.insert(0,greeting_list[2])

but seems the last line of code is wrong but in python shell it works fine. How to add an item in the first position of a list? What is the correct answer for this task?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

In your code:

name_list.insert(0,greeting_list[2])

You are inserting the third item of greeting_list into the first position of name_list which results in

['Treehouse', 'Daniel', 'Chikaka']

Instead you want to do the reverse by replacing the third item in greeting_list with the first item of name_list

greeting_list[2] = name_list[0]

ooh yes! i didn't pay attention to the word "swap" ...thanks