Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How to get a value from a list of tuples? (Python)?

I have a list of tuples and I want to be able to access them but list[i][j] where i is the position in the list and j the position of the number in the tuple doesn t work

Update:

I did count starting with 0 but still I get a syntax error

Update 2:

What is the syntax error?

Update 3:

The problem is I want to access a value from a list of tuples in Python but list[0][1] for example doesn't work. Which is the correct way to do it? That's what I'm trying to say

1 Answer

Relevance
  • 6 years ago

    That is the correct syntax, and should work, but do NOT use "list" as a variable name. That's a built-in function name. After you create a local variable named list, you won't be able to use the builtin function without a __builtins__. prefix. This could mess up other parts of your code.

    One easy way to see names to avoid is to run the following in interactive mode (or at the Idle shell prompt):

    dir(__builtins__)

    ...or for a slightly easier to read version:

    print ( "\t".join( dir(__builtins__) ) )

    Back to the syntax error, Python should be showing you the line it occurred on. If you are running a .py source file from the command line, and it's not clear to you what the complaint is, try Idle. Launch it, the open your .py file with the File>Open (Ctrl+O) and the run it with Run>Run Module (F5). Idle will put you back in the editor with the cursor at the exact character where the syntax error was detected.

    Other errors than syntax errors give a message you can right-click on and "go to file/line" to get to the line where the error was detected. (The cause may have been somewhere else, though.)

Still have questions? Get your answers by asking now.