Python Forum
Python Error List Index Out of Range
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Error List Index Out of Range
I am making a program using python and tkinter which basically is an expense tracking system. However, I am getting an error when running my program which has to do with the code shown below. It is raising an error, mentioning that the 'list index is out of range.'

It confuses me that I am getting this error because the for loop (in this case) runs only once and the index seems to be within range as the value of 'i' is 1 and the list that it is iterating through is as follows: [('admin', '999'), ('Cheese', 'incorrect')] Would anyone please be able to explain why I am getting this error...

        for i in range(1, len(self.l_credentials)):
            self.my_users.add_command(label="{}".format(self.l_credentials[i][0]), command=lambda: self.callback(self.l_credentials[i][0]))
buran write Sep-03-2021, 05:18 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
index start at 0 and thus max index value is len(self.l_credentials)-1
However, check this

Also, always post full traceback you get, in error tags, not just the last line
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

The only way I could get your code to raise and index out of range error is put an empty list or tuple in l_credentials.

I would write your example like this:
for creds in self.l_credentials:
    if len(creds> 0):
        self.my_users.add_command(creds[0], command=lambda arg=creds[0]: self.callback(arg))
Are you skipping the first credential on purpose? This starts at l_credentials[1], the second credential.
for i in range(1, len(self.l_credentials))
Do you want to bind all commands in my_users to the last user. That is what you are doing with this lambda expression.
self.my_users.add_command(label="{}".format(self.l_credentials[i][0]), command=lambda: self.callback(self.l_credentials[i][0]))
See this example for proof. It looks like it should print "admin" , "Cheese", "Milk".
l_credentials =  [('admin', '999'), ('Cheese', 'incorrect'), ('Milk', 'Cookies')]
funcs = [lambda: print(l_credentials[i][0]) for i in range(len(l_credentials))]
for func in funcs:
Milk Milk Milk
My list comprehension makes a list of functions that call print(l_credentials[i][0]) and that is the problem. When I execute my functions, i == 2, and each function in my comprehension is evaluated as print(l_credentials[2][0]) Your code will do the same. If you have 10 l_credentials, every command will call self.callback(self.l_credentials[9][0]).

There are two ways to get around this: You can use partial from the functools library, or you can use default arguments in your lambda expression. In this example the lambda expression creates an argument named "arg" and assigns it the default value of l_credentials[i][0] for the CURRENT value of i.
l_credentials =  [('admin', '999'), ('Cheese', 'incorrect'), ('Milk', 'Cookies')]
funcs = [lambda arg=l_credentials[i][0]: print(arg) for i in range(len(l_credentials))]
for func in funcs:
admin Cheese Milk
This works because each of my lambdas has a different default value for the first argument. If we could peek into the lamda, you would see:
funcs = [lambda: print('admin'), lambda: print('Cheese'), lambda: print('Milk')
Note that this works just like a default argument. If an argument value is provided in the function call, the default value is overwritten.
l_credentials =  [('admin', '999'), ('Cheese', 'incorrect'), ('Milk', 'Cookies')]
funcs = [lambda arg=l_credentials[i][0]: print(arg) for i in range(len(l_credentials))]
for func in funcs:
    func('All the same again')
All the same again All the same again All the same again
Awesome, this solved my problem! I appreciate it, thanks! Also, sorry for forgetting to include the tags for my code, if that is what you're referring to, but I am not sure how to do that

Possibly Related Threads…
Thread Author Replies Views Last Post
  matplotlib x axis range goes over the set range Pedroski55 5 517 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  IndexError: list index out of range rf_kartal 6 742 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  Index error - columns vs non-column Vinny 3 1,773 Aug-09-2021, 04:46 PM
Last Post: snippsat
  IndexError: list index out of range Laplace12 1 830 Jun-22-2021, 10:47 AM
Last Post: Yoriz
  python error: bad character range \|-t at position 12 faustineaiden 0 793 May-28-2021, 09:38 AM
Last Post: faustineaiden
  How to resolve Index Error in my code? codify110 6 1,188 May-22-2021, 11:04 AM
Last Post: supuflounder
  IndexError: list index out of range brunolelli 11 2,298 Mar-25-2021, 11:36 PM
Last Post: brunolelli
  index of range, but data prints out mrc06405j 1 778 Mar-25-2021, 07:20 PM
Last Post: buran
  Changing Index of 2 List in python giddyhead 0 702 Mar-05-2021, 05:45 PM
Last Post: giddyhead
  IndexError: list index out of range ramu4651 2 1,380 Jan-24-2021, 01:45 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020