0
for i in range (len(userCards)):
  print("i is "+str(userCards[i]))
  if userCards.count(userCards[i]) == 4:
    userMatches = userMatches + 1
    userCards.remove(userCards[i])

I'm trying to remove duplicate cards from a list of numbers but I keep getting the list index out of range error

  • It actually looks like you want to remove elements that appear exactly 4 times in your list. If so, you can follow a similar route to [this answer](https://stackoverflow.com/a/26773120/5858851). – pault Jun 27 '18 at 21:23
  • I'm confused on where to add the changes to the code, whenever I run my program, it still gives me the same error – YEETetoo Jun 28 '18 at 01:50
  • Did you read the linked duplicates? They explain in more detail, but essentially when you call `remove` you are reducing the length of your list as you are iterating over it. Eventually you remove enough items and `i` will be out of range. Change your `print` statement to: `print("i is {index} and length is {len}".format(index=i, len=len(userCards)))` and that should show you what's going on. – pault Jun 28 '18 at 02:05

0 Answers0