Open In App
Related Articles
• Write an Interview Experience
• Python Programs

# Python – Convert key-values list to flat dictionary

Sometimes, while working with Python dictionaries, we can have a problem in which we need to flatten dictionary of key-value pair pairing the equal index elements together. This can have utilities in web development and Data Science domain. Lets discuss certain way in which this task can be performed.

### Convert key-values list to flat dictionary using zip() + dict()

The combination of above functions can be used to achieve the required task. In this, we perform the pairing using zip() and dict() is used to convert tuple data returned by zip() to dictionary format.

## Python3

 `# Python3 code to demonstrate working of``# Convert key-values list to flat dictionary``# Using dict() + zip()``from` `itertools ``import` `product` `# initializing dictionary``test_dict ``=` `{``'month'` `: [``1``, ``2``, ``3``],``             ``'name'` `: [``'Jan'``, ``'Feb'``, ``'March'``]}` `# printing original dictionary``print``("The original dictionary ``is` `: " ``+` `str``(test_dict))` `# Convert key-values list to flat dictionary``# Using dict() + zip()``res ``=` `dict``(``zip``(test_dict[``'month'``], test_dict[``'name'``]))` `# printing result``print``("Flattened dictionary : " ``+` `str``(res))`

Output :

The original dictionary is : {‘name’: [‘Jan’, ‘Feb’, ‘March’], ‘month’: [1, 2, 3]} Flattened dictionary : {1: ‘Jan’, 2: ‘Feb’, 3: ‘March’}

Time Complexity: O(n)
Auxiliary Space: O(n)

## Python3

 `# Python3 code to demonstrate working of``# Convert key-values list to flat dictionary``# initializing dictionary``test_dict ``=` `{``'month'` `: [``1``, ``2``, ``3``],``            ``'name'` `: [``'Jan'``, ``'Feb'``, ``'March'``]}` `# printing original dictionary``print``(``"The original dictionary is : "` `+` `str``(test_dict))` `# Convert key-values list to flat dictionary``x``=``list``(test_dict.values())``a``=``x[``0``]``b``=``x[``1``]``d``=``dict``()``for` `i ``in` `range``(``0``,``len``(a)):``    ``d[a[i]]``=``b[i]``# printing result``print``(``"Flattened dictionary : "` `+` `str``(d))`

Output

```The original dictionary is : {'month': [1, 2, 3], 'name': ['Jan', 'Feb', 'March']}
Flattened dictionary : {1: 'Jan', 2: 'Feb', 3: 'March'}
```

The time complexity of this program is O(n), where n is the length of the longest list value in the dictionary.

The auxiliary space complexity of this program is O(n), where n is the length of the longest list value in the dictionary.

Convert key-values list to flat dictionary Using a dictionary comprehension

This method iterates over the indices of the ‘month’ list, and for each index i, it creates a key-value pair in the new dictionary where the key is the i-th element of the ‘month’ list, and the value is the i-th element of the ‘name’ list.

## Python3

 `test_dict ``=` `{``'month'``: [``1``, ``2``, ``3``], ``'name'``: [``'Jan'``, ``'Feb'``, ``'March'``]}``res ``=` `{test_dict[``'month'``][i]: test_dict[``'name'``][i] ``for` `i ``in` `range``(``len``(test_dict[``'month'``]))}``print``(``"Flattened dictionary:"``, res)`

Output

```Flattened dictionary: {1: 'Jan', 2: 'Feb', 3: 'March'}
```

Time complexity: O(N), where N is the length of the ‘month’ list.
Auxiliary space: O(N), since we are creating a new dictionary that has the same number of key-value pairs as the ‘month’ list.

### Use a for loop to iterate over the keys and values and create a new dictionary by assigning each key-value pair to the new dictionary.

Step-by-step approach :

• Initialize a dictionary test_dict with two keys ‘month’ and ‘name’, where the values are lists of integers and strings respectively.
• Print the original dictionary using print() and str() functions.
• Create an empty dictionary res to store the flattened key-value pairs.
• Iterate over the range of the length of the ‘month’ list using a for loop.
• For each iteration, use the current index i to access the i-th elements of the ‘month’ and ‘name’ lists using their respective keys in the test_dict dictionary.
• Assign the key-value pair to the res dictionary using the key from the ‘month’ list and the corresponding value from the ‘name’ list.
• Print the resulting flattened dictionary using print() and str() functions.

Below is the implementation of the above approach:

## Python3

 `# Python3 code to demonstrate working of``# Convert key-values list to flat dictionary``# Using for loop` `# initializing dictionary``test_dict ``=` `{``'month'` `: [``1``, ``2``, ``3``],``             ``'name'` `: [``'Jan'``, ``'Feb'``, ``'March'``]}` `# printing original dictionary``print``(``"The original dictionary is : "` `+` `str``(test_dict))` `# Convert key-values list to flat dictionary``# Using for loop``res ``=` `{}``for` `i ``in` `range``(``len``(test_dict[``'month'``])):``    ``res[test_dict[``'month'``][i]] ``=` `test_dict[``'name'``][i]` `# printing result``print``(``"Flattened dictionary : "` `+` `str``(res))`

Output

```The original dictionary is : {'month': [1, 2, 3], 'name': ['Jan', 'Feb', 'March']}
Flattened dictionary : {1: 'Jan', 2: 'Feb', 3: 'March'}
```

Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), as we are creating a new dictionary to store the flattened key-value pairs.