Python's list.pop half off sale

Question:

I’m trying to pop all the elements in a list one by one over every iteration, problem is Python is only popping the upper half of the list and I need it to pop the all of it. Here’s my code:

cards = ['As', 'Ks', 'Qs', 'Js', '10s', '9s', '8s', '7s', '6s', '5s', '4s', '3s', '2s',
        'Ad', 'Kd', 'Qd', 'Jd', '10d', '9d', '8d', '7d', '6d', '5d', '4d', '3d', '2d',
        'Ac', 'Kc', 'Qc', 'Jc', '10c', '9c', '8c', '7c', '6c', '5c', '4c', '3c', '2c',
        'Ah', 'Kh', 'Qh', 'Jh', '10h', '9h', '8h', '7h', '6h', '5h', '4h', '3h', '2h']

        #itertools.combinations returns a tuple and we store it in tupOnePlayerAllCombos
        tupOnePlayerAllCombos = itertools.combinations(cards, 2)

        #We need to convert out tuple to a list in order for us to use the replace() methos, tuples do not have such a method
        lstOnePlayerAllCombos = list(tupOnePlayerAllCombos)

        #There are characters we need to delete from each list item, we will store each edited list item in this list
        lstEditedOnePlayerAllCombos = []        


        #replace all useless characters in every list item (   '   and   (   and   )   and   ,   )
        for lstOnePlayerAllCombo in lstOnePlayerAllCombos:
            lstOnePlayerAllCombo = str(lstOnePlayerAllCombo).replace("'","").replace("(","").replace(")","").replace(", ","")           

            #Add edited list item to our new list
            lstEditedOnePlayerAllCombos.append(lstOnePlayerAllCombo)
            lstEditedOnePlayerAllCombos2 = lstEditedOnePlayerAllCombos

        #For every list item in our combination list
        for lstEditedOnePlayerAllCombo in lstEditedOnePlayerAllCombos:

            #We need to delete the current list item from the list, so that we dont get duplicate hands for other players
            #so we retrieve the index by searching the list with our current value and store that value as our player1 hand         
            strPlayerOneHand = lstEditedOnePlayerAllCombos2.pop(lstEditedOnePlayerAllCombos.index(lstEditedOnePlayerAllCombo))
            intLength = (len(lstEditedOnePlayerAllCombos2)-1)

            #print (lstEditedOnePlayerAllCombos)
            print("The current length of the list is " + str(intLength))
Asked By: Tray Tray

||

Answers:

You can remove the contents of the entire list like this

my_list = [1, 2, 3, 4, 5]
del my_list[:]
print my_list
# []

In Python 3.x, you can use list.clear method, like this

my_list = [1, 2, 3, 4, 5]
my_list.clear()
print(my_list)
# []

If you want to pop all the elements one by one, on each iteration, then

my_list = [1, 2, 3, 4, 5]
while my_list:
    print my_list.pop()
# 5
# 4
# 3
# 2
# 1
Answered By: thefourtheye
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.