0

I am randomly picking a value from a list and then removing it from the original list. I am doing this within a function and the list is out of the function. I print the list and it is in the list but, it still says it isn't.

import random
CC = ["T:G","N:F"]
CCr = CC
random.shuffle(CCr)
def Get(CCran):
   print("CC: ", CC)
   if len(CCr) > 0:
       card = CCran[0]
       del CCran[0]
   else:
       random.shuffle(CC)
       CCran = CC
       card = CCran[0]
       del CCran[0]
   Act = card[0:2]
   if Act == "T:":
       print("T")
       #Below gives a value error.
       CC.remove("T:G")
Get(CCr)

What am I doing wrong?

Hawkeye
  • 377
  • 1
  • 3
  • 14
  • 3
    `CCran = CC` doesn't make a copy: `CCran = CC[:]` does. – Jean-François Fabre Jan 05 '17 at 18:49
  • I've paved the way for the dupehammer. Anyone ? – Jean-François Fabre Jan 05 '17 at 18:49
  • It's not really a duplicate, but that certainly could be a bug. The code isn't clear enough to tell if the lack of proper copies vs. alias is actually the issue with respect to the original intent of the code, but it is highly probable that the code isn't doing what the author expects in any case. – aruisdante Jan 05 '17 at 18:52
  • While it is a bit of a headache to read this code due to not following naming style conventions, poor choice of variable names, etc, I think this is obviously a case of the OP thinking they are making copies when they are indeed not. They are explicitly deleting an element form a list and then trying to remove it from the same list using two different names. – juanpa.arrivillaga Jan 05 '17 at 18:54
  • I'm trying to understand your code here. It looks like the goal of Get() is to take a random item out of the list, is that correct? – calico_ Jan 05 '17 at 18:58

0 Answers0