1

I have just recently began to learn python. I am trying to find the lowest integer in the digits list. Instead I get this error:

Traceback (most recent call last):
  File "H:/Python/untitled/08_python.py", line 32, in <module>
    if digits[e] < temp:
IndexError: list index out of range

I realize that you can use min(digits) but I thought I would challenge my knowledge so far. I most likely made a simple mistake and am not diagnosing it properly. If I throw 0 in the list everything works fine.

digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

low = 0
temp = 0
for e in digits:
    if digits[e] < temp:
        low = digits[e]
    temp = digits[e]

print(low)
2
  • 2
    e is each of the digits in the list. You can't just use those as index into digits again, you don't have a digits[10]. Commented Mar 29, 2016 at 16:50
  • 1
    Just use if e < temp and temp = e. You don't need both a temp and low, by the way. Commented Mar 29, 2016 at 16:51

3 Answers 3

6

for e in digits iterates over the contents of the list directly. You don't need to use digits[e] at that point since e is already the value, not the index. And that's also why it fails, since digits[10] is past the end of your list.

Other minor note - your code will only work with positive numbers since you're starting with low = 0; if the list has negatives only you'll still get 0 as your result.
One way to avoid that by setting it to None at the beginning and checking for that as well:

low = None
for e in digits:
    if low is None or low < e:
        low = e
print(low)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I didn't know how to declare an integer as none
Python is dynamically typed; you don't need to declare variables and you can assign the same name to different types. low is not an integer or a None, it's just a name referring to one of those things. Here is a really good article on that.
@RylandScarth: the code doesn't declare anything. Python variables don't care about the type of object that are assigned to them; low = None is just an assignment, not a declaration. So first low is bound to the None object, then later it is bound to an integer, and the variable never cares. You can assign a string to it later on if you like.
2

for e in digits translates roughly as:

for i in range(0, len(digits)):
    e = digits[i]

So your code becomes:

for i in range(0, len(digits)):
    if digits[digits[i]] < temp:

When:

  • i == 0, digits[i] == 1, & digits[digits[i]] == 2
  • i == 1, digits[i] == 2, & digits[digits[i]] == 3
  • i == 8, digits[i] == 9, & digits[digits[i]] == 10
  • i == 9, digits[i] == 10, & digits[digits[i]] raises IndexError

When you add in 0 at the start of the list, then the identity i == digits[i] holds for all indices, and your code doesn't crash, but it is still doing the double indexing, which is not what you want.

Comments

2

You have several problems in your code:

  • You are making the assumption e is an index. It is not, it is the actual value from your digits list. Your list has indices from 0 through to 9, so when e == 10 (the last element) you'll get an IndexError; there is no digits[10].

  • You set temp and low to 0. None of your values in digits are lower than 0 so you'll never find anything where e < temp will be true. You'll need to pick a very high value instead. float('inf') is an artificial 'highest' value (infinity), that works really well for such tests; any integer value is lower than infinity.

  • You don't need both a temp and a low variable. You only need one or the other to test if a later value is lower still.

Working code, then, looks like this:

low = float('inf')
for e in digits:
    if e < low:
        low = e

Comments

Your Answer

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.