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
erik Jones Oniga
3,307 Pointsremoving numbers from a list
Hi guys,
let's say, I have a list like that:
my_list = [1, 2, 3, 4] then I want that 3 and 4 to be removed.
how can I code that..
like that?
my_list.remove([3, 4])?
I mean, I know how to remove a list that is in my list, just like that:
list_in_list = [1, 2, 3, 4[5, 6,]] then I have to use the following code:
list_in_list.remove([5, 6]) = [1, 2, 3, 4]
but if i do
my_list = [1, 2, 3,4]
which code do i need to use, in order to remove 3 and 4?
Thanks guys.
Bruno Correia
3,114 PointsBruno Correia
3,114 PointsHi Erik,
Unfortunately the remove() method takes one argument only, which is why you can't use it to solve this problem in one line as you seem to want. While it does indeed work if you want to remove a list that is inside another list, that's because the list you put in parenthesis, like list_in_list.remove([5, 6]) is actually one argument only. That argument (list) happens to have 2 values, but it's still one list.
That said, you can achieve what you need with a couple different options (and even more):
Write 2 lines of code to remove one value at a time by writing the values you wish to remove:
Use del two times based on the index the values you want to delete are located at:
If you know about slices already, this is a good example where they would make your life easier by just needing to write one line of code (again, using the index and not the actual values that are part of the list). Remember slices are exclusive. There is no index 4 but if you replace it by 3 the del function will not delete index 3 of the list.
del my_list[2:4]Hope this helps!