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
#1
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)):
            print(i)
            print(self.l_credentials)
            self.my_users.add_command(label="{}".format(self.l_credentials[i][0]), command=lambda: self.callback(self.l_credentials[i][0]))
            self.my_users.add_separator()
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.
Reply
#2
index start at 0 and thus max index value is len(self.l_credentials)-1
However, check this https://python-forum.io/thread-362.html

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

Reply
#3
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:
    func()
Output:
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:
    func()
Output:
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')
Output:
All the same again All the same again All the same again
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyscript index error while calling input from html form pyscript_dude 2 938 May-21-2023, 08:17 AM
Last Post: snippsat
  Index error help MRsquared 1 738 May-15-2023, 03:28 PM
Last Post: buran
Thumbs Down I hate "List index out of range" Melen 20 3,154 May-14-2023, 06:43 AM
Last Post: deanhystad
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 5,955 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  IndexError: list index out of range dolac 4 1,843 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,277 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,408 May-03-2022, 01:39 PM
Last Post: Anldra12
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,499 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  matplotlib x axis range goes over the set range Pedroski55 5 3,101 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  IndexError: list index out of range rf_kartal 6 2,759 Sep-07-2021, 02:36 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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