3
def Task4():
 import random
 t = True
 list1 = []
 list2 = []
 rand = random.randint(1,6)
 list1.append(rand)
 print(list1)
 for x in range(0,5):
 if list1[0] == (rand):
 list1.pop(0)
 else:
 list2.append(rand)
 print(list2)
 list1.pop(0)

Can't workout why if list1[0] == (rand): keeps coming up with a list index out of range.

vaultah
46.8k12 gold badges120 silver badges145 bronze badges
asked Oct 18, 2015 at 12:17
3
  • You need to check that the first element exists (i.e. the list isn't empty) before indexing it -> if list1 and list1[0] == rand Commented Oct 18, 2015 at 12:20
  • Debug your code and you'll know why. You can debug it using a pencil and a paper. Commented Oct 18, 2015 at 12:20
  • You appended to list1 only one element but in the loop you tried to pop 5 elements. Commented Oct 18, 2015 at 12:24

3 Answers 3

3

Let’s see what happens to list1:

list1 = []

Ok, the list is created, and it’s empty.

rand = random.randint(1,6)
list1.append(rand)

rand is added to the list, so at this point list1 = [rand].

for x in range(0,5):

This will loop four times; so let’s take a look at the first iteration:

if list1[0] == (rand):

Since list1 = [rand], list1[0] is rand. So this condition is true.

list1.pop(0)

The list element at index 0 is removed; since list1 only contained one element (rand), it’s now empty again.

for x in range(0,5):

Second iteration of the loop, x is 1.

if list1[0] == (rand):

list1 is still empty, so there is no index 0 in the list. So this crashes with an exception.


At this point, I would really like to tell you how to solve the task in a better way, but you didn’t specify what you are trying to do, so I can only give you a hint:

When you remove items from a list, only iterate for as often as the list contains elements. You can either do that using a while len(list1) loop (which will loop until the list is empty), or by explicitely looping over the indexes for i in len(list1). Of course, you can also avoid removing elements from the list and just loop over the items directly using for x in list1.

answered Oct 18, 2015 at 12:25

Comments

0
  • when x is 0: -> you pop the only existing list item, thus the list is empty afterwards

  • when x is 1: -> you try to access list1[0], which doesn't exist -> list index out of range error

answered Oct 18, 2015 at 12:25

2 Comments

Dear Downvoter, I'm interested in improving my answers, thus: What's so bad about the answer that you downvoted it? Yes, it could certainly be more complete, but given that it's a simple question, I thought a simple answer would do.
I think your answer is perfect. Curious about the downvote as well. Anyway, have my upvote.
0
truth is evident with debugging with print and try except:
def Task4():
 import random
 t = True
 list1 = []
 list2 = []
 rand = random.randint(1,6)
 list1.append(rand)
 for x in range(0,5):
 try:
 print("list1 before if and pop",list1)
 if list1[0] == (rand):
 list1.pop(0)
 print("list1 after pop",list1)
 else:
 list2.append(rand)
 print("list2 {}".format(list2))
 list1.pop(0)
 except IndexError as e:
 print("Index Error {}".format(e))
 break
Task4()
list1 before if and pop [3]
list1 after pop []
list1 before if and pop []
Index Error list index out of range
answered Oct 18, 2015 at 12:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.