Skip to main content

Python Dictionary Operations

Dictionaries are fundamental data structures in Python. These utilities help you transform, map, and convert dictionary data efficiently.

Convert Between Lists and Dictionaries

Dictionary to List

Convert a dictionary to a list of tuples:
def dict_to_list(d):
  return list(d.items())

d = {'one': 1, 'three': 3, 'five': 5, 'two': 2, 'four': 4}
dict_to_list(d)
# [('one', 1), ('three', 3), ('five', 5), ('two', 2), ('four', 4)]

List to Dictionary

Combine two lists into a dictionary:
def to_dictionary(keys, values):
  return dict(zip(keys, values))

to_dictionary(['a', 'b'], [1, 2]) # {'a': 1, 'b': 2}

Map List to Dictionary

Apply a function to create key-value pairs:
def map_dictionary(itr, fn):
  return dict(zip(itr, map(fn, itr)))

map_dictionary([1, 2, 3], lambda x: x * x) # {1: 1, 2: 4, 3: 9}

Transform Dictionary Values

Apply a function to all values in a dictionary:
def map_values(obj, fn):
  return dict((k, fn(v)) for k, v in obj.items())

users = {
  'fred': { 'user': 'fred', 'age': 40 },
  'pebbles': { 'user': 'pebbles', 'age': 1 }
}
map_values(users, lambda u : u['age']) # {'fred': 40, 'pebbles': 1}

Sort Dictionary List

Sort a list of dictionaries using multiple keys:
friends =  [
  {"name": "John", "surname": "Doe", "age": 26},
  {"name": "Jane", "surname": "Doe", "age": 28},
  {"name": "Adam", "surname": "Smith", "age": 30},
  {"name": "Michael", "surname": "Jones", "age": 28}
]

print(
  sorted(
    friends,
    key=lambda friend:
    (friend["age"], friend["surname"], friend["name"])
  )
)
# [
#   {'name': 'John', 'surname': 'Doe', 'age': 26},
#   {'name': 'Jane', 'surname': 'Doe', 'age': 28},
#   {'name': 'Michael', 'surname': 'Jones', 'age': 28},
#   {'name': 'Adam', 'surname': 'Smith', 'age': 30}
# ]

Practical Use Cases

Extract Specific Fields

Extract ages from user objects:
users = {
  'alice': {'name': 'Alice', 'age': 30},
  'bob': {'name': 'Bob', 'age': 25}
}
ages = map_values(users, lambda u: u['age'])
# {'alice': 30, 'bob': 25}

Create Lookup Tables

Build a lookup table from lists:
ids = ['user1', 'user2', 'user3']
names = ['Alice', 'Bob', 'Charlie']
lookup = to_dictionary(ids, names)
# {'user1': 'Alice', 'user2': 'Bob', 'user3': 'Charlie'}

Transform API Responses

Convert API data structures:
api_data = {'key1': 'value1', 'key2': 'value2'}
list_format = dict_to_list(api_data)
# [('key1', 'value1'), ('key2', 'value2')]

Multi-level Sorting

Sort complex data structures:
employees = [
  {'dept': 'Sales', 'name': 'John', 'salary': 50000},
  {'dept': 'Sales', 'name': 'Jane', 'salary': 60000},
  {'dept': 'IT', 'name': 'Bob', 'salary': 55000}
]

sorted_employees = sorted(
  employees,
  key=lambda e: (e['dept'], -e['salary'], e['name'])
)
# First by department, then by salary (descending), then by name

Next Steps

Lists

Learn about list operations

Strings

Explore string manipulation utilities

Build docs developers (and LLMs) love