Loading...

Python Nested Data

order keeps two names, and the value of each name is a list. lida's list is tea, then rice. guest's list is only water. The dictionary is counting names. Foods sitting inside a list are not extra names.

The first brackets choose the name. order["lida"] is the whole list, tea then rice, not the single word tea. The second brackets choose a place in the list you just reached. Place 0 is tea. Place 1 is rice. Name first, place second.

tea, then rice, then two names

python
order = {"lida": ["tea", "rice"], "guest": ["water"]}

print(order["lida"][0])
print(order["lida"][1])
print(len(order))

The screen shows tea, then rice, then 2. The first two lines are the two places under lida. The 2 is how many names order has. tea and rice share one name, so they do not become two names.

guest's list is still there, and it holds water. Nothing from guest was printed, because the program never read order["guest"]. Reading lida left guest alone.

order["guest"][0] would be water. order["guest"][1] stops. That list has one place, and its number is 0. Place 1 is not there. This read does not come back as None. The program stops.

order["soup"][0] stops at the first brackets. soup is not a name, so there is no list to step into. The second brackets never run.

order["lida"][0] = "soup" would put soup where tea was. rice would stay at place 1. guest would stay water. len(order) would stay 2. One food changed. The set of names did not.

The next page pulls several values out into their own names. This page only steps from a name into the list stored under it.

What the order holds

  1. What does the program write?
  2. What is order["lida"] before the second brackets?
  3. What would order["guest"][0] be?
  4. What happens if you write order["guest"][1]?
  5. What happens if you write order["soup"][0]?

Two foods, one name

The screen is tea, then rice, then 2. The 2 counts names. guest still holds water and was not printed. A place that is not there stops the program. A missing name stops at the first brackets. The next page pulls values out into their own names.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
6
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top