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!
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
Paul Nam2,093 Points
lists.py: I've watched the video many times, I don't know what to do.
I'm not understanding how to pass this code challenge. Please if anyone can help. i've viewed the video constantly.
states = [ 'ACTIVE', ['red', 'green', 'blue'], 'CANCELLED', 'FINISHED', 5, ]
The remove function is actually pretty simple, you call it on the list you want to remove something from and then you pass it the exact thing you want removed from the list. Here is an example:
exampleList = ["Hello", 5, 6, 12, [1, 6, 3]] # Say I want to remove the number 5 from that list, all I have to do is this: exampleList.remove(5) # Same for strings: exampleList.remove("Hello") # This is true even for more complicated values like the nested list exampleList.remove([1, 6 , 3]) # After running those three commands the list now looks like this: # [6, 12]
As you can see all I'm doing is calling the remove function and providing it with the exact same value that I want to remove from the list, you can think of it as a function that does the exact opposite of the append function. Instead of adding the exact thing you specify, it will instead remove it.
In this challenge you are asked to remove the number 5 during the first task and a nested list in the second task, using the examples I have provided above as guidance it should be possible to solve the challenge relatively easily.
Dzumret Pelivani5,399 Points
In the challenge we are to remove the last item in the list. What is confusing it that remove function does not care about last item. Remove function is concerned only about the value of the argument you pass to it.
Remove function removes the first item that matches the provided argument. As soon as the function finds item you tell it to remove in the list of items. Item is removed and function 'stops'.
Example how remove works:
# list of colors with 'red' color three times colors = ['blue', 'red', 'green', 'red', 'black', 'red'] # we want to remove 'red' items in the list # with the remove function colors.remove('red') # print colors list to check the result print(colors) # the first occurrence of 'red' color is removed # and colors list looks like this now ['blue', 'green', 'red', 'black', 'red'] # if we use remove function one more time colors.remove('red') # and print colors list to check the result again print(colors) # the first occurrence of 'red' color is removed one more time ['blue', 'green', 'black', 'red']
Example shows that remove function removes the first item it 'finds' with the value of 'red'. So red color in the end is still there so to remove 'red' in the end we can call remove one more time
colors.remove('red') # print colors list to check the result print(colors) # red is gone ['blue', 'green', 'black']
Remove function is built in functionality of lists, strings and other sequence types, documentation.
Hope this can help you understand lists built in function remove.