Fiveable

๐ŸIntro to Python Programming Unit 10 Review

QR code for Intro to Python Programming practice questions

10.2 Dictionary creation

๐ŸIntro to Python Programming
Unit 10 Review

10.2 Dictionary creation

Written by the Fiveable Content Team โ€ข Last updated September 2025
Written by the Fiveable Content Team โ€ข Last updated September 2025
๐ŸIntro to Python Programming
Unit & Topic Study Guides

Dictionaries in Python are versatile data structures for storing key-value pairs. They offer flexible creation methods, from curly braces to the dict() function, allowing easy initialization with various data types.

Advanced dictionary operations enhance their utility. Comprehensions, iteration methods, and lookup techniques make dictionaries powerful tools for organizing and accessing data efficiently in Python programs.

Dictionary Creation in Python

Dictionary creation with curly braces

  • Create dictionaries using curly braces {} with initial key-value pairs
    • Define unique keys and their corresponding values separated by colons
      • (my_dict = {'name': 'John', 'age': 25, 'city': 'New York'})
  • Keys must be unique and immutable objects (strings, numbers, tuples)
    • Duplicate keys result in the last assigned value being used
  • Values can be any data type (mutable or immutable) and can be duplicated
    • ({'a': 1, 'b': [2, 3], 'c': 'hello', 'd': (4, 5)})
    • Nested dictionaries can be created by using dictionaries as values

Dict() function for data conversion

  • Convert other data types into dictionaries using the dict() function
    • Lists of tuples can be converted to dictionaries
      • Each tuple should contain exactly two elements (key, value)
        • (dict([('name', 'John'), ('age', 25), ('city', 'New York')])
    • Keyword arguments can create dictionaries
      • Argument names become keys and argument values become dictionary values
        • (dict(name='John', age=25, city='New York'))
  • Keyword arguments used with dict() must be valid Python identifiers
    • No spaces or special characters, except underscore (_)

Methods of dictionary initialization

  • Create empty dictionaries using curly braces or dict() without arguments
    • Curly braces: my_dict = {}
    • Dict() function: my_dict = dict()
  • Initialize dictionaries with key-value pairs using curly braces or dict()
    • Curly braces: my_dict = {'key1': value1, 'key2': value2}
    • Dict() with keyword arguments: my_dict = dict(key1=value1, key2=value2)
    • Dict() with list of tuples: my_dict = dict([('key1', value1), ('key2', value2)])
  • Choose the appropriate dictionary creation method based on the use case and initial data format
    • Curly braces for simple key-value pairs
    • Dict() for converting other data types or using keyword arguments

Advanced Dictionary Operations

  • Dictionary comprehension allows for concise creation of dictionaries based on existing iterables
  • Iteration over dictionaries can be done using methods like .keys(), .values(), or .items()
  • Key lookup is performed using square brackets or the .get() method, which can provide default values
    • Example: my_dict.get('key', default_value)