Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (Retired) Lists Redux Manipulating Lists

Jovan Dandridge
Jovan Dandridge
12,835 Points

Manipulating List 3 of 3 not working... or is there something wrong?

im not able to get this to work and I did everything the video says

lists.py
the_list = ["a", 2, 3, 1, False, [1, 2, 3]]

# Your code goes below here
list = the_list
list.pop(3)
list.insert(0, 1)
list.remove("a")
list.remove(False)
list.remove([1, 2, 3])
list.extend(range(4, 21))

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,989 Points

The challenge is looking for the variable the_list. Your code manipulates a list instead. Rename all your statements to use the_list.

Chris Freeman
Chris Freeman
Treehouse Moderator 67,989 Points

Jovan, technically your code produces the correct answer. The grader is not smart enough to know it.

When you copied the list using list = the_list, you are actually only copying the reference to the list. An item can be identified by its "id". In the code below, you can see that list and the_list have the same id and the same value at the end of the code:

the_list = ["a", 2, 3, 1, False, [1, 2, 3]]
print("the_list", id(the_list), the_list)

list = the_list
print("list", id(list), list)

list.pop(3)
print("list", id(list), list)

list.insert(0, 1)
print("list", id(list), list)

list.remove("a")
print("list", id(list), list)

list.remove(False)
print("list", id(list), list)

list.remove([1, 2, 3])
print("list", id(list), list)

list.extend(range(4, 21))
print("list", id(list), list)
print("the_list", id(the_list), the_list)

Results:

$ python3
Python 3.4.3 (default, Oct 14 2015, 20:28:29) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> the_list = ["a", 2, 3, 1, False, [1, 2, 3]]
>>> print("the_list", id(the_list), the_list)
the_list 139645670348872 ['a', 2, 3, 1, False, [1, 2, 3]]
>>> 
>>> list = the_list
>>> print("list", id(list), list)
list 139645670348872 ['a', 2, 3, 1, False, [1, 2, 3]]
>>> 
>>> list.pop(3)
1
>>> print("list", id(list), list)
list 139645670348872 ['a', 2, 3, False, [1, 2, 3]]
>>> 
>>> list.insert(0, 1)
>>> print("list", id(list), list)
list 139645670348872 [1, 'a', 2, 3, False, [1, 2, 3]]
>>> 
>>> list.remove("a")
>>> print("list", id(list), list)
list 139645670348872 [1, 2, 3, False, [1, 2, 3]]
>>> 
>>> list.remove(False)
>>> print("list", id(list), list)
list 139645670348872 [1, 2, 3, [1, 2, 3]]
>>> 
>>> list.remove([1, 2, 3])
>>> print("list", id(list), list)
list 139645670348872 [1, 2, 3]
>>> 
>>> list.extend(range(4, 21))
>>> print("list", id(list), list)
list 139645670348872 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
>>> print("the_list", id(the_list), the_list)
the_list 139645670348872 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
Jovan Dandridge
Jovan Dandridge
12,835 Points

Thanks Chris Freeman it works.