Open In App

Append Dictionary Keys and Values ( In order ) in Dictionary – Python

Last Updated : 23 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Appending dictionary keys and values in order ensures that the sequence in which they are added is preserved. For example, when working with separate lists of keys and values, we might want to append them in a specific order to build a coherent dictionary. Let’s explore several methods to achieve this.

Using zip and Dictionary Constructor

This is the most efficient and commonly used method to append keys and values in order.

# Initialize lists of keys and values
keys = ["name", "age", "city"]
values = ["Alice", 30, "New York"]

# Create a dictionary by zipping keys and values
d = dict(zip(keys, values))

# Print the dictionary
print(d)

Output
{'name': 'Alice', 'age': 30, 'city': 'New York'}

Explanation:

  • zip() function pairs each key with its corresponding value in a single step.
  • dict() constructor efficiently creates a dictionary from the paired elements.
  • This method is highly efficient because it combines operations into one concise line.

Using for Loop with Direct Assignment

This method involves manually iterating over the keys and values to append them in order.

# Initialize lists of keys and values
keys = ["name", "age", "city"]
values = ["Alice", 30, "New York"]

# Initialize an empty dictionary
d = {}

# Append keys and values in order
for k, v in zip(keys, values):
    d[k] = v

# Print the dictionary
print(d)

Output
{'name': 'Alice', 'age': 30, 'city': 'New York'}

Explanation:

  • zip() function combines keys and values for iteration.
  • Each key-value pair is appended to the dictionary using direct assignment.

Using update() with a Dictionary Comprehension

This method uses a dictionary comprehension to create key-value pairs and appends them to an existing dictionary using the update method.

# Initialize lists of keys and values
keys = ["name", "age", "city"]
values = ["Alice", 30, "New York"]

# Initialize an empty dictionary
d = {}

# Append keys and values using dictionary comprehension and update
d.update({k: v for k, v in zip(keys, values)})

# Print the dictionary
print(d)

Output
{'name': 'Alice', 'age': 30, 'city': 'New York'}

Explanation:

  • A dictionary comprehension generates the key-value pairs from the zip output.
  • The update method appends these pairs to the dictionary.
  • This method is useful for situations where keys and values are generated dynamically.

Using OrderedDict from collections

This method is useful if maintaining the order is critical, especially when working with Python versions prior to 3.7.

from collections import OrderedDict

# Initialize lists of keys and values
keys = ["name", "age", "city"]
values = ["Alice", 30, "New York"]

# Create an OrderedDict by zipping keys and values
d = OrderedDict(zip(keys, values))

# Print the dictionary
print(d)

Output
OrderedDict({'name': 'Alice', 'age': 30, 'city': 'New York'})

Explanation:

  • OrderedDict() ensures that the keys are stored in the order they are inserted.
  • It behaves like a regular dictionary but provides guaranteed order preservation.
  • While slightly less efficient than standard dictionaries, it is valuable in older Python versions.

Using a List of Tuples and dict Constructor

This method creates a list of tuples representing key-value pairs and converts it to a dictionary.

# Initialize lists of keys and values
keys = ["name", "age", "city"]
values = ["Alice", 30, "New York"]

# Create a dictionary using a list of tuples
d = dict([(k, v) for k, v in zip(keys, values)])

# Print the dictionary
print(d)

Output
{'name': 'Alice', 'age': 30, 'city': 'New York'}

Explanation:

  • A list of key-value tuples is generated using a list comprehension.
  • The dict constructor converts the list of tuples into a dictionary.
  • This method is less efficient due to the intermediate creation of a list.


Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg