-1

I'm trying to generate a random list of 7 values and want to check the list for duplicate numbers and remove those from the list. I currently have:

import random
acard1 = random.randrange(0,13,1)
acard2 = random.randrange(0,13,1)
acard3 = random.randrange(0,13,1)
acard4 = random.randrange(0,13,1)
acard5 = random.randrange(0,13,1)
acard6 = random.randrange(0,13,1)
acard7 = random.randrange(0,13,1)
myhand=[acard1, acard2, acard3, acard4, acard5, acard6, acard7]
print(myhand)

How would I check if there are repeated values and then remove those values?

For example, if my list were [11, 7, 11, 12, 9, 9, 10], how would I have my program recognize that 11 and 9 are repeated and turn the list into [7, 12, 10]?

phd
  • 82,685
  • 13
  • 120
  • 165
A Blak
  • 11

2 Answers2

2

Here's a simple technique using collections.Counter:

In [451]: from collections import Counter

In [453]: c = Counter(l)

In [454]: [x for x in l if c[x] == 1]
Out[454]: [7, 12, 10]

Any answers with a list comprehension using l.count(x) are definitely going to be slower than this.

cs95
  • 379,657
  • 97
  • 704
  • 746
0

Maybe create a list where you keep unique entries by checking if index number is taken? Make index 1 contain the number 1, index 2 contains number 2, and so on.

Alternatively you could search through the entire list to check if 11 is already present somewhere. Depending on language and type of list, you could delete the element at the specific index, og simply append to a new "unique" list as i attempted to describe above.