Dictionary Functions

Core Dictionary Methods funtions

1. clear()

  • Syntax: dict.clear()

  • Description: Removes all items from the dictionary, leaving it empty.

  • Example:

    Python

    d = {'a': 1, 'b': 2}
    d.clear()
    print(d)  # Output: {}
    

2. copy()

  • Syntax: dict.copy()

  • Description: Returns a shallow copy of the dictionary.

  • Example:

    Python

    d = {'a': 1}
    d_copy = d.copy()
    print(d_copy)  # Output: {'a': 1}
    

3. fromkeys()

  • Syntax: dict.fromkeys(sequence[, value])

  • Description: Creates a new dictionary with keys from the given sequence and values set to a specified default (defaults to None). IE IT GIVES THE SAME VALUE TO ALL OF THEM

  • Example:

    Python

    keys = ['a', 'b', 'c']
    d = dict.fromkeys(keys, 0)
    print(d)  # Output: {'a': 0, 'b': 0, 'c': 0}
    

    if we wanted to linnk two lists, we use the zip function Dictionary Functions > 12. `zip()`

4. get()

  • Syntax: dict.get(key[, default])

  • Description: Returns the value for a specified key. If the key doesn’t exist, it returns default (which defaults to None) instead of raising a KeyError.

  • Example:

    Python

    d = {'name': 'Alice'}
    print(d.get('name'))        # Output: Alice
    print(d.get('age', 25))     # Output: 25 (key doesn't exist, returns default)
    

5. items()

  • Syntax: dict.items()

  • Description: Returns a dynamic view object displaying a list of a dictionary’s key-value tuple pairs.

  • Example:

    Python

    d = {'a': 1, 'b': 2}
    print(list(d.items()))  # Output: [('a', 1), ('b', 2)]
    

6. keys()

  • Syntax: dict.keys()

  • Description: Returns a view object displaying a list of all the keys in the dictionary.

  • Example:

    Python

    d = {'a': 1, 'b': 2}
    print(list(d.keys()))  # Output: ['a', 'b']

7. pop()

  • Syntax: dict.pop(key[, default])

  • Description: Removes the specified key and returns its value. If the key is not found, it returns default (if provided), otherwise it raises a KeyError.

  • Example:

    Python

    d = {'a': 1, 'b': 2}
    val = d.pop('a')
    print(val)  # Output: 1
    print(d)    # Output: {'b': 2}
    

8. popitem()

  • Syntax: dict.popitem()

  • Description: Removes and returns the last inserted key-value pair as a tuple (LIFO order). Raises a KeyError if the dictionary is empty.

  • Example:

    Python

    d = {'a': 1, 'b': 2}
    item = d.popitem()
    print(item)  # Output: ('b', 2)
    

9. setdefault()

  • Syntax: dict.setdefault(key[, default])

  • Description: Returns the value of a key if it exists. If not, it inserts the key with a specified default value (which defaults to None) and returns it.

  • Example:

    Python

    d = {'a': 1}
    val = d.setdefault('b', 5)
    print(val)  # Output: 5
    print(d)    # Output: {'a': 1, 'b': 5}
    

10. update()

  • Syntax: dict.update([other])

  • Description: Updates the dictionary with elements from another dictionary object or from an iterable of key-value pairs. Overwrites existing keys.

  • Example:

    Python

    d = {'a': 1}
    d.update({'a': 10, 'b': 2})
    print(d)  # Output: {'a': 10, 'b': 2}
    

11. values()

  • Syntax: dict.values()

  • Description: Returns a view object displaying a list of all the values in the dictionary.

  • Example:

    Python

    d = {'a': 1, 'b': 2}
    print(list(d.values()))  # Output: [1, 2]
    

12. zip()

to map two lists 1-1, we use the zip function

keys = ["a", "b", "c"] 
values = [1, 2, 3] # This maps them index-by-index 
result = dict(zip(keys, values))
print(result) # Output: {'a': 1, 'b': 2, 'c': 3}

Modern Dictionary Operators (Python 3.9+)

Python also supports operators for merging and updating dictionaries natively:

  • Merge (|): Combines two dictionaries into a new one.

    Python

    d1 = {'a': 1}
    d2 = {'b': 2}
    merged = d1 | d2  # Output: {'a': 1, 'b': 2}
    
  • Update in-place (|=): Updates a dictionary with another dictionary’s contents.

    Python

    d1 = {'a': 1}
    d1 |= {'b': 2}  # d1 becomes {'a': 1, 'b': 2}
    

Note: Dictionary views (keys(), values(), and items()) are dynamic—meaning if the dictionary changes, the view reflects those changes automatically.

Would you like to explore how to iterate through dictionaries efficiently using these methods?
col1000 python dictionary