Python Forum
list indices must be integers or slices, not lists error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list indices must be integers or slices, not lists error
#1
For the following code:
def create_ID(tmp):
    try_again = True
    in_list = False
    while try_again == True:
        New_ID = input("Please enter the new ID")
        y = 0
        for x in tmp:
            if New_ID in tmp[x][0]: # checks if ID is already in list row by row
                print("That ID is already on the list") # if it is return this error message 
                in_list = True
                y = y + 1
        if in_list == False: # if ID is not in the list close the loop
            try_again = False
    return New_ID
I'm getting this error:

Error:
Traceback (most recent call last): File "c:/Users/djwil/Documents/python/learning python/Chapter 19 - Chunky Challenges/Passwords.py", line 173, in <module> main() File "c:/Users/djwil/Documents/python/learning python/Chapter 19 - Chunky Challenges/Passwords.py", line 150, in main New_ID = create_ID(tmp) # gets New_ID from create_ID File "c:/Users/djwil/Documents/python/learning python/Chapter 19 - Chunky Challenges/Passwords.py", line 18, in create_ID if New_ID in tmp[x][0]: # checks if ID is already in list row by row TypeError: list indices must be integers or slices, not list
But I think that I'm defining it as an integer of the a specific place in the list, can someone offer advice?
Reply
#2
After for x in tmp it should probably be if New_Id in x[0]
Reply
#3
If tmp is a list of lists. for x in tmp makes x a list. You cannot use a list as an index into a list. I think you want to do this:
for x in tmp:
    if new_id == x[0]:
tmp is a bad variable name. It tells me nothing about what the variable is or how I should use it. I think that you would have fewer problems if you started using better variable names.

You should also think about what type of data structures best fit your problem. I don't know anything this challenge, but if you used a dictionary instead of a list of lists your test for unique password is:
if passwords.get(new_id):
    print("That ID is already on the list")
else:
    break;
And one last quibble. Printing is not the same as returning.
print("That ID is already on the list") # if it is return this error message 
Misleading comments are far worse than no comment at all.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tuple indices must be integers or slices, not str cybertooth 16 11,071 Nov-02-2023, 01:20 PM
Last Post: brewer32
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,091 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,181 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,256 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,178 Mar-24-2023, 08:34 AM
Last Post: fullytotal
Question How to append integers from file to list? Milan 8 1,358 Mar-11-2023, 10:59 PM
Last Post: DeaD_EyE
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 1,930 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Error "list indices must be integers or slices, not str" dee 2 1,393 Dec-30-2022, 05:38 PM
Last Post: dee
  user input values into list of lists tauros73 3 1,023 Dec-29-2022, 05:54 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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