0

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))
Tray Tray
  • 25
  • 8
  • Do you want to empty the entire list? – thefourtheye Apr 26 '14 at 05:49
  • And another dupe: [Why does a for-loop with pop-method (or del statement) not iterate over all list elements](http://stackoverflow.com/q/13939341/748858) – mgilson Apr 26 '14 at 06:09
  • 1
    This seems like it may be an [XY problem](http://meta.stackexchange.com/a/66378). Your issue with `pop` and iteration isn't really the issue you're trying to solve, just an obstacle you ran into. What seems you really want to do is find multiple pairs of cards without any duplicates between them. If you want help with that issue (rather than just the `pop` issue), you should add information about what you actually want to iterate over (hands of a game with a given number of players?). There's probably a more direct way to get the specific iteration you need. – Blckknght Apr 26 '14 at 06:40

1 Answers1

0

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
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • Im sorry, I wasn't specific, I want to pop each element into a variable on each iteration and delete that element from the list in the same iteration. – Tray Tray Apr 26 '14 at 05:56
  • Ooo... I didn't realize they had added `list.clear` in py3k. Excellent. Excellent indeed. (I always wondered why it didn't have it in python2.x to be consistent with other builtin types ...) – mgilson Apr 26 '14 at 06:14
  • This is my first time on the forum, sorry bout that....accepted – Tray Tray Apr 26 '14 at 07:12