0
theListofSalary = [
["     $0 -   $9,999  ",": "]
["$10,000 -   $19,999 ",": "]
["$20,000 -   $29,999 ",": "]
["$30,000 -   $39,999 ",": "]
["$40,000 -   $49,999 ",": "]
["$50,000 -   $59,999 ",": "]
["$60,000 -   $69,999 ",": "]
["$70,000 -   $79,999 ",": "]
["$80,000 -   $89,999 ",": "]
["$90,000 -   $99,999 ",": "]
["$100,000 - $149,999 ",": "]
["$150,000 and over   ",": "]
]

As the title says I have no idea why the list of list is giving me this error. I tried to search up the error but I'm new and unable to understand what they are saying.

1

You miss , after each list element

theListofSalary = [
    ["     $0 -   $9,999  ",": "], 
    ["$10,000 -   $19,999 ",": "],
    ....
0

You are missing the commas between items of your main list.

theListofSalary = [
    ["     $0 -   $9,999  ",": "],
    ["$10,000 -   $19,999 ",": "],
    ["$20,000 -   $29,999 ",": "],
    ["$30,000 -   $39,999 ",": "],
    ["$40,000 -   $49,999 ",": "],
    ["$50,000 -   $59,999 ",": "],
    ["$60,000 -   $69,999 ",": "],
    ["$70,000 -   $79,999 ",": "],
    ["$80,000 -   $89,999 ",": "],
    ["$90,000 -   $99,999 ",": "],
    ["$100,000 - $149,999 ",": "],
    ["$150,000 and over   ",": "]
]

You will notice I added commas to all but the last one, and this should work now :)

  • thx so much sorry for wasting ur time lol – Akira Patafio Mar 12 at 1:26
  • @AkiraPatafio No problem. This is a situation where IDE's are your best friend. As soon as I copy/pasted it into mine, it became clear what the problem was from the way it was highlighted. Maybe you could look into using PyCharm (what I use) since it is free. There are many other great IDEs out there though! – Reedinationer Mar 12 at 1:32
0

You're missing commas (,) after each entry in your list declaration, as the other answers have stated. However, the other answers don't explain why you're seeing the strange error.

What's happening is that Python thinks you're trying to index into the first inner list using the second inner list, which happens to be a tuple data type (or a comma-separated immutable list).

print([""][0])    # this works and prints ""
print([""][0, 0]) # TypeError: list indices must be integers, not tuple

This is important to understand, because the following code causes no problems for the interpreter and could lead to very subtle bugs in a large program:

theListofSalary = [
    ["     $0 -   $9,999  "]
    [0]
    [0]
    [0]
    [0]
    [0]
    [0]
    [0]
    [0]
    [0]
    [0]
    [0]
]
print(theListofSalary)

The above code prints [' ']. Can you see why?

Another example:

theListofSalary = [
    ["     $0 -   $9,999  "]
    [0]
    [12]
]
print(theListofSalary)

prints ['$']. Again, can you see why this works?

theListofSalary = [
    ["     $0 -   $9,999  "]
    [0]
    [80]
]
print(theListofSalary)

In the above example, we get IndexError: string index out of range.

theListofSalary = [
    ["     $0 -   $9,999  "]
    [0]
    ["hello world"]
]
print(theListofSalary)

The above example emits TypeError: string indices must be integers.

Hopefully you're beginning to see the pattern here! Long story short, make sure to add commas to your list declarations so the interpreter doesn't mistake your sub-lists as indexing operations on the first sub-list.

Last but not least, the Python style guide suggests snake_cased_variable_names. Titling a list as the_list_of_salaries is needlessly verbose; I recommend calling this list salaries, simply.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.