Concepts / Working with Python Dictionaries and Lists

Working with Python Dictionaries and Lists

json.loads() returns native Python data structures (lists and dictionaries), not JSON text.

  • Programming

From JSON Text to Python Data

When JSON is received as text, it is not yet a Python list or dictionary that you can navigate with brackets. The json.loads() function parses that text and returns native Python data structures. Once parsing is complete, you work with the result using ordinary Python indexing rather than JSON-specific extraction methods.

inputreturnsJSON textraw stringjson.loads()parsePython listcontains dictionaries
What changes when json.loads() converts raw JSON text into Python data?

Reading the Parsed Shape

The first step in extracting data is to understand the structure returned by parsing. In the source example, the variable info is a list containing two elements. The first element, info[0], is a dictionary with the keys id, x, and name. This tells you which kind of operation to use at each level: use a numeric index for the list, then use a key for the dictionary.

StructureIdentifier usedSource expression
ListNumeric positioninfo[0]
DictionaryKeyitem['key']
usesusesListindex: 0First elementinfo[0]Dictionarykey: nameChuckinfo[0]['name']
How are dictionary keys different from list indexes when identifying and retrieving values?

Following Each Bracket

Finding the First User's Name

Retrieve the name stored in the first dictionary inside the parsed variable info.

Start with the list: The variable info is a list. The expression info[0] selects its first element.

Inspect the selected element: The first element is a dictionary containing the keys id, x, and name.

Select the dictionary value: The expression info[0]['name'] uses the name key to retrieve the value associated with it.

The expression info[0]['name'] retrieves 'Chuck'.

info[0] info[0]['name']

[0]selects['name']infotop-level list0first elementdictionaryid, x, namenameChuck
How does Python move through each list index and dictionary key to reach the target value?

Native Python After Parsing

After json.loads() has returned the parsed value, extraction uses Python syntax. You do not need to call the JSON library again to retrieve a value. Standard Python indexing, loops, and methods can work with the returned native structures.

parses toparses toJSON arraystructured JSON valuePython listindex accessJSON objectkeyed JSON valuePython dictionarykey access
Which native Python structure corresponds to each structured JSON form described in this lesson?

Inspect the structure returned by parsing before writing a longer extraction expression. Knowing whether the outer value is a list or dictionary tells you whether the next operation should use a numeric position or a key.

Mistakes with Nested Access

  • Treating the parsed result as if it were still raw JSON text.

    The parsed result is a native Python structure, so its values should be retrieved with Python indexing.

    Fix: Use expressions such as info[0] and info[0]['name'].

  • Using a dictionary key where a list position is required.

    The first level is a list, and the list must be narrowed to one element before its dictionary keys can be used.

    Fix: Use the two stages: first info[0], then the dictionary key.

  • Using a list index where a dictionary key is required.

    The selected element is a dictionary whose values are identified by keys.

    Fix: Use info[0]['name'] to retrieve the value under the name key.

Practice the Navigation

EASY

Suppose info is the parsed list described in this lesson. Write the Python expression that selects the first element, and then write the chained expression that retrieves the first element's name value.

Hints
  • The first list element uses index 0.
  • The dictionary value is selected with the name key.
  • The complete chained expression combines both bracket operations.

What do you think happens?

What value does info[0]['name'] retrieve in the source example?

  • The first user's name, Chuck
  • The whole list
  • The first dictionary's id key
Reveal answer

Answer: The first user's name, Chuck

info[0] selects the first dictionary, and ['name'] selects the value associated with that dictionary key.

Key Takeaways

  1. json.loads() converts JSON text into native Python structures rather than leaving the result as JSON text.
  2. A list is navigated by numeric position, such as info[0].
  3. A dictionary is navigated by key, such as item['key'].
  4. Chained indexing moves through nested structures one level at a time.
  5. After parsing, ordinary Python indexing, loops, and methods are used for data extraction.

Key Takeaways

  • json.loads() returns native Python lists and dictionaries from parsed JSON data.
  • Use numeric indexes to select list elements and keys to select dictionary values.
  • Read chained expressions from left to right, narrowing the structure at each bracket operation.
  • Once parsing is complete, extract values with standard Python syntax rather than JSON-specific methods.