Bird
Raised Fist0
Pythonprogramming~5 mins

Formatting structured data in Python - Cheat Sheet & Quick Revision

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is structured data in programming?
Structured data is organized information that follows a specific format, like lists, dictionaries, or tables, making it easy to access and manipulate.
Click to reveal answer
beginner
Which Python module helps format structured data into readable text or JSON?
The json module formats data into JSON strings, and the pprint module helps print data structures in a readable way.
Click to reveal answer
intermediate
How does the json.dumps() function help with formatting?
json.dumps() converts Python data structures into a JSON string, which can be formatted with indentation for readability.
Click to reveal answer
beginner
What does the indent parameter do in json.dumps()?
The indent parameter adds spaces to format the JSON string with new lines and indentation, making it easier to read.
Click to reveal answer
intermediate
Why use pprint.pprint() instead of print() for complex data?
pprint.pprint() prints complex data structures like nested dictionaries in a neat, indented way, improving readability compared to print().
Click to reveal answer
Which Python module is best for converting data to JSON format?
Amath
Bos
Cjson
Drandom
What does the indent parameter in json.dumps() control?
ANumber of spaces for indentation
BNumber of items to include
CData type conversion
DSorting keys alphabetically
Which function prints data structures in a readable, formatted way?
Aprint()
Bpprint.pprint()
Cinput()
Dlen()
What type of data is considered structured data?
AAudio files
BRandom text
CImages
DLists and dictionaries
Which method converts Python objects into a JSON string?
Ajson.dumps()
Bjson.load()
Cpprint.pprint()
Dstr()
Explain how to format a Python dictionary into a readable JSON string.
Think about how to make the output easy to read with spaces and new lines.
You got /4 concepts.
    Describe the difference between using print() and pprint.pprint() for structured data.
    Consider how complex data looks when printed simply versus formatted.
    You got /4 concepts.

      Practice

      (1/5)
      1. What does the indent parameter do in json.dumps() when formatting structured data?
      easy
      A. Adds spaces and new lines to make the output easier to read
      B. Converts data into a binary format
      C. Removes all spaces and new lines for compact output
      D. Encrypts the JSON data for security

      Solution

      1. Step 1: Understand the purpose of json.dumps()

        This function converts Python data into a JSON string.
      2. Step 2: Role of indent parameter

        The indent parameter adds spaces and new lines to format the JSON string nicely for readability.
      3. Final Answer:

        Adds spaces and new lines to make the output easier to read -> Option A
      4. Quick Check:

        indent = readable JSON [OK]
      Hint: Remember: indent means pretty print with spaces [OK]
      Common Mistakes:
      • Thinking indent compresses JSON
      • Confusing indent with encryption
      • Assuming indent changes data content
      2. Which of the following is the correct syntax to format a Python dictionary data as a JSON string with indentation of 4 spaces?
      easy
      A. json.dumps(data, indent=4)
      B. json.dumps(data, indent='4')
      C. json.dumps(data, indent=True)
      D. json.dumps(data, space=4)

      Solution

      1. Step 1: Check the correct parameter type for indent

        The indent parameter expects an integer number of spaces, not a string or boolean.
      2. Step 2: Validate each option

        json.dumps(data, indent=4) uses indent=4 correctly. json.dumps(data, indent='4') uses a string '4' which is invalid. json.dumps(data, indent=True) uses boolean True which is invalid. json.dumps(data, space=4) uses a wrong parameter name space.
      3. Final Answer:

        json.dumps(data, indent=4) -> Option A
      4. Quick Check:

        Indent value must be integer [OK]
      Hint: Use integer for indent, not string or boolean [OK]
      Common Mistakes:
      • Passing indent as a string instead of integer
      • Using wrong parameter name like 'space'
      • Passing boolean instead of number
      3. What is the output of this code?
      import json
      data = {'name': 'Alice', 'age': 30}
      print(json.dumps(data, indent=2))
      medium
      A. {"name": "Alice", "age": 30}
      B. { "name": "Alice", "age": 30 }
      C. {name: Alice, age: 30}
      D. SyntaxError

      Solution

      1. Step 1: Understand json.dumps with indent=2

        The function converts the dictionary to a JSON string with 2 spaces indentation for each nested level.
      2. Step 2: Check the output format

        The output will have new lines and spaces, keys and string values in double quotes, and numeric values as is.
      3. Final Answer:

        { "name": "Alice", "age": 30 } -> Option B
      4. Quick Check:

        Indent=2 adds spaces and new lines [OK]
      Hint: Indent adds new lines and spaces for readability [OK]
      Common Mistakes:
      • Expecting compact JSON without spaces
      • Using single quotes instead of double quotes
      • Confusing Python dict print with JSON string
      4. The following code throws an error. What is the mistake?
      import json
      data = {'x': 1, 'y': 2}
      print(json.dumps(data, indent=2.0))
      medium
      A. json.dumps requires a second argument for separators
      B. The data dictionary keys must be strings only
      C. json.dumps cannot format dictionaries
      D. The indent parameter should be an integer, not a float

      Solution

      1. Step 1: Identify the error cause

        The indent parameter is given as a float 2.0 instead of an integer 2.
      2. Step 2: Understand parameter type requirements

        json.dumps expects indent to be an integer number of spaces for formatting, passing a float causes a TypeError.
      3. Final Answer:

        The indent parameter should be an integer, not a float -> Option D
      4. Quick Check:

        Indent must be int, not float [OK]
      Hint: Check indent type: must be integer, not float [OK]
      Common Mistakes:
      • Passing indent as float instead of int
      • Thinking keys must be strings for json.dumps
      • Expecting separators argument is mandatory
      5. You have a list of dictionaries representing users:
      users = [{'name': 'Bob', 'age': 25}, {'name': 'Eve', 'age': 28}]

      How do you create a JSON string with indentation of 2 spaces and keys sorted alphabetically?
      hard
      A. json.dumps(users, indent='2', sort_keys=True)
      B. json.dumps(users, indent=2, sort=True)
      C. json.dumps(users, indent=2, sort_keys=True)
      D. json.dumps(users, pretty=2, sort_keys=True)

      Solution

      1. Step 1: Use json.dumps with indent and sort_keys

        The indent parameter formats the JSON with spaces and new lines. The sort_keys=True sorts dictionary keys alphabetically.
      2. Step 2: Validate correct parameter names and types

        json.dumps(users, indent=2, sort_keys=True) uses correct parameters and types. json.dumps(users, indent=2, sort=True) uses invalid parameter sort. json.dumps(users, indent='2', sort_keys=True) passes indent as string which is invalid. json.dumps(users, pretty=2, sort_keys=True) uses invalid parameter pretty.
      3. Final Answer:

        json.dumps(users, indent=2, sort_keys=True) -> Option C
      4. Quick Check:

        Use indent=int and sort_keys=True [OK]
      Hint: Use sort_keys=True and indent as integer [OK]
      Common Mistakes:
      • Using wrong parameter names like sort or pretty
      • Passing indent as string
      • Forgetting sort_keys to sort keys