How to do a double loop and removing things from a list


#1



HOW TO DO A DOUBLE LOOP AND REMOVING THINGS FROM A LIST

I would like to learn about the double loop technics. i would like to compare some integers into a list by doing a double loop and deletings the values during the progression of the loop.
the more i will progress in my loop the more my list will be small


in order to avoid to be out of range i think we have to do a loop from the last index to the first. dont forget my main goal is to remore stuff from the list

print list1

...i just want to learn and i expect the code to work well


Replace this line with your code. 

list1=[["bread",5],["banana",34],["milk",16.5],["lettuce",22.5],["coco",22.5],["coke",34],["cheese",19],["honey",16.5],["apple",34]]



for i in range(len(list1)):
   for j in list1[-1:i:]:
        if list1[i][1] < list1[i][j]:
            list1.
            
            
            
print list1


#2

A lot of times it's better for performance/readability to create a new list instead of removing.

If you're removing from the end then that's fine performance-wise, but if you're not, then there's a whole lot of moving going on each time.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.