Storing lists within lists in Python

2024/11/16 12:07:54

I have a question about accessing elements in lists.

This is the code:

movies = ["The Holy Grail", 1975, "Terry Jones and Terry Gilliam", 91,["Graham Champman", ["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"]]]print(movies[4][1][3])

And this is the output: Eric Idle

My question is why is the output Eric Idle? What does 4 represent, what do 1 and 3 represent? I'm so confused.

Answer

Your list is separated into values.

# movies: values
0. "The Holy Grail"
1. 1975
2. "Terry Jones and Terry Gilliam"
3. 91
4. ["Graham Champman", ["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"]]

/!\ The index begin from 0

The last value is also separated into values:

# movies[4]: values
0. "Graham Champman"
1. ["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"]

And the last value is also separated into other values:

# movies[4][1]: values
0. "Michael Palin",
1. "John Cleese"
2. "Terry Gilliam"
3. "Eric Idle"
4. "Terry Jones"

So calling movies[4] returns the last element of movies:

["Graham Champman", ["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"]]

Typing movies[4][1] returns this:

["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"]

And typing movies[4][1][3] returns that:

"Eric Idle"

Tree view

movies0. | "The Holy Grail"1. | 19752. | "Terry Jones and Terry Gilliam"3. | 914. |____4.0. | "Graham Champman"4.1. |____4.1.0 | "Michael Palin"4.1.1 | "John Cleese"4.1.2 | "Terry Gilliam"4.1.3 | "Eric Idle"4.1.4 | "Terry Jones"

Hope that helped.

https://en.xdnf.cn/q/120330.html

Related Q&A

getting ZeroDivisionError: integer division or modulo by zero

I had written a simple pascal triangle code in python but I am getting a errordef factorial(n):c=1re=1for c in range(n):re = re * c;return(re)print "Enter how many rows of pascal triangle u want t…

How to scrape images from a website and display them on html file?

I am scraping images from https://www.open2study.com/courses I got all the image sources but dont know how to display the images (instead of links) on a table with 2 column ( one column for title and o…

Multiple files comparing using python [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

parsing interactive broker fundamental data

Ive successfully pulled data from IB using the api. It comes in XML format and it looks like this...<TotalRevenues currency="USD"><TotalRevenue asofDate="2017-12-31" report…

How to format HTTP request to discord API?

While this code works, it sends "Bad Request". import socket, ssl token = NzMyMzQ1MTcwNjK2MTR5OEU3.XrzQug.BQzbrckR-THB9eRwZi3Dn08BWrM HOST = "discord.com" PORT = 443 t = POST / HTTP…

Python 3.30 TypeError: object of type int has no len() [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…

Is it possible to disable negative indexing? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 7 years ago.Improve…

Google Colab Notebook completely freezes when training a YOLO model

I am following a tutorial to train custom object detection model using YOLO. This is the tutorial and also where I got the Notebook Everything works fine until the training bit which is the last cell. …

Generate 4 columns of data such that each row sum to 100

How do I write a python program that can randomly generate 4 columns of data such that the sum of the numbers of each row is 100?

Nan to Num Python

I have multiple array that for those I calculate a linear regression, but sometimes it gives me 0/0 values which gives me a NaN. I know that to convert an array where there are numbers that are NaN you…