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
#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


Messages In This Thread
RE: Python Error List Index Out of Range - by buran - Sep-03-2021, 05:22 PM
RE: Python Error List Index Out of Range - by deanhystad - Sep-03-2021, 06:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pyscript index error while calling input from html form pyscript_dude 2 1,011 May-21-2023, 08:17 AM
Last Post: snippsat
  Index error help MRsquared 1 793 May-15-2023, 03:28 PM
Last Post: buran
Thumbs Down I hate "List index out of range" Melen 20 3,395 May-14-2023, 06:43 AM
Last Post: deanhystad
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 6,506 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  IndexError: list index out of range dolac 4 1,956 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,386 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,470 May-03-2022, 01:39 PM
Last Post: Anldra12
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,622 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  matplotlib x axis range goes over the set range Pedroski55 5 3,268 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  IndexError: list index out of range rf_kartal 6 2,888 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