Create a List using Custom Key-Value Pair of a Dictionary - Python
Last Updated :
28 Jan, 2025
The task of creating a list using custom key-value pairs from a dictionary involves extracting the dictionary’s keys and values and organizing them into a list of tuples. This allows for the flexibility of representing data in a sequence that maintains the relationship between keys and values.
For example, if we have a dictionary like {'a': 1, 'b': 2, 'c': 3}, the goal is to convert it into a list like [('a', 1), ('b', 2), ('c', 3)].
Using zip()
zip() combines two or more iterables into tuples. To create a list from a dictionary's key-value pairs, we can use zip() to combine the keys and values separately and then create the desired list. Although the zip() is useful in certain scenarios, it's less common for creating lists of key-value pairs from a dictionary directly.
Python
d = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
key = d.keys()
val = d.values()
res = list(zip(key, val))
print(res)
Output[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
Explanation:
- d.keys() retrieves the keys from the dictionary d .
- d.values() retrieves the values from the dictionary d .
- zip() combines the two iterables into pairs. It pairs the first key with the first value, the second key with the second value and so on.
Using map()
map() allows us to apply a transformation to each element in an iterable. While it's more commonly used with lists, we can combine map() with a lambda function to extract and create custom key-value pairs from a dictionary.
Python
d = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
res = list(map(lambda item: (item[0], item[1]), d.items()))
print(res)
Output[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
Explanation:
- d.items() returns key-value pairs as tuples: ('a', 1), ('b', 2), ('c', 3), ('d', 4).
- map() applies the lambda function to each item, extracting the key and value as a tuple.
- list() converts the result to a list.
Using list comprehension
List comprehension is a efficient way to construct lists. It’s particularly useful when we need to process or transform items from an iterable. Using list comprehension, we can efficiently extract custom key-value pairs and create the desired list:
Python
d= {'a': 1, 'b': 2, 'c': 3, 'd': 4}
res = [(key, val) for key, val in d.items()]
print(res)
Output[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
Explanation: List Comprehension iterates over the dictionary's key-value pairs and creates a list of tuples.
Using loop
for loop is one of the most straightforward and flexible ways to extract key-value pairs from a dictionary. It allows us to iterate over the dictionary's items explicitly, providing full control over the process of extracting and transforming data.
Python
d= {'a': 1, 'b': 2, 'c': 3, 'd': 4}
res= [] # initialized an empty list
for key, val in d.items():
res.append((key, val))
print(res)
Output[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
Explanation:
for
loop iterates over each key-value pair in the dictionary d .- .items() returns a sequence of tuples, each containing a key and its associated value.
Similar Reads
Python - Create a Dictionary using List with None Values The task of creating a dictionary from a list of keys in Python involves transforming a list of elements into a dictionary where each element becomes a key. Each key is typically assigned a default value, such as None, which can be updated later. For example, if we have a list like ["A", "B", "C"],
3 min read
Python - Add custom values key in List of dictionaries The task of adding custom values as keys in a list of dictionaries involves inserting a new key-value pair into each dictionary within the list. In Python, dictionaries are mutable, meaning the key-value pairs can be modified or added easily. When working with a list of dictionaries, the goal is to
5 min read
Add a key value pair to Dictionary in Python The task of adding a key-value pair to a dictionary in Python involves inserting new pairs or updating existing ones. This operation allows us to expand the dictionary by adding new entries or modify the value of an existing key.For example, starting with dictionary d = {'key1': 'geeks', 'key2': 'fo
3 min read
Convert Dictionary String Values to List of Dictionaries - Python We are given a dictionary where the values are strings containing delimiter-separated values and the task is to split each string into separate values and convert them into a list of dictionaries, with each dictionary containing a key-value pair for each separated value. For example: consider this d
2 min read
Python - Key Value list pairings in Dictionary Sometimes, while working with Python dictionaries, we can have problems in which we need to pair all the keys with all values to form a dictionary with all possible pairings. This can have application in many domains including day-day programming. Lets discuss certain ways in which this task can be
7 min read