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 Collections (2016, retired 2019) Lists Pop

horus93
horus93
4,333 Points

Is there a better way to delete multiple items at distinct points within a list?

Is there a better way to delete multiple items at distinct points within maybe one line of code instead of what I've had to do here with two for the challenge?

=========================== messy_list = ["a", 2, 3, 1, False, [1, 2, 3]]

Your code goes below here

edit = messy_list.pop(3)

messy_list.insert(0, edit)

del messy_list[1] del messy_list[3:5]

print (messy_list)

because we wind up having to delete the "a", False, and the sublist, but with there being a series of other index places between the "a" and other index's you're trying to cut out of the list further on. I used a slice to save myself from repeating the del function again, but if it's possible I'd like to know if there's a way to do it with one line (since, the function's want a single argument).

I was thinking of using pop to trade places with the "a" and the 1, but again, single argument so i'd be repeating code there for that, putting in a second pop and insert, and reducing my del usage down to one, but altogether still gaining one line of code extra.

Or maybe I did do it as efficiently as possible, but I find it unlikely due to my still infant level of knowledge regarding the subject matter.

2 Answers

Mustafa Başaran
Mustafa Başaran
28,046 Points

Hi Horus,

You can delete multiple items in a list with a loop. Within the loop, you can iterate over the list items and check conditions with an if, elif and else construction. Remove items based on these conditions.
On the video, Kenneth uses a while loop.

Another interesting way to filter and subset rows of data would be using python's pandas library.

horus93
horus93
4,333 Points

I mean, yes I suppose that's fine but that only works if you know exactly what those conditions are right? Or can you make those conditions variable based on user input? That's really what I'm trying to figure out here, how I could take the deleting and moving of objects within the messy_list and have the user tell it to remove x, y, or z items/item exactly within the list, but right now im not sure how to even approach doing that without having it index based, and having it based on index would be confusing to users not familiar with programming so that's not really a good option.

Ideally here, i'd want it to receive a user input on what to delete, and give them choices of picking whether they wanted to delete one instance of the item (assuming there's multiples), or all instances of the item, and then, if it's just one instance they want to delete (and there happens to be multiple copies of the same item on the list) then they'd be able to point to it somehow and excise it from say the middle while leaving the other copies alone.