Convert a dictionary of lists to a flat dictionary in Python

Updated

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

Example :

Input :
test_dict = {'Word' : ['W3C', 'CS', 'W3Colleges'],
                   'Character_Count' : [3, 2, 10]}
Output : {'W3C': 3, 'CS': 2, 'W3Colleges': 10}

This conversion can be performed by the below approach:

Approach :

Follow the below steps to understand the approach better.

  1. Step-1: Firstly, Initialise a dictionary with key and list values.
  2. Step-2: Secondly, print the original dictionary.
  3. Step-3: Then, declare another dictionary that will store the elements grouped together.
  4. Step-4: Use zip() function to get the elements in the dictionary in the form of a tuple.
  5. Step-5: Use dict() function to convert the tuple to a dictionary.
  6. Step-6: Lastly, print the new dictionary as the result.

The zip() function is used to return a zip object that is an iterator of tuples where the first item in each passed iterator is paired together, and then the second item in each passed iterator is paired together, etc.

If the passed iterators have different lengths, the iterator with the least items decides the length of the new iterator.

Syntax :

zip(iterator1, iterator2, iterator3 ...)

Parameters :

This function accepts some parameters which are illustrated below:

  • iterator1, iterator2, iterator3 …: This is the specified Iterator objects that will be joined together

The dict() function is used to create a dictionary. An example of the created dictionary is as given below:

dict(Company = "W3C", Word_Count = 3, Department = "CSE")

Syntax :

dict(keyword arguments)

Parameters :

This function accepts some parameters which are illustrated below:

  • keyword arguments: This is the as many keyword arguments that are separated by a comma.

Example : In the below example, the combination of the zip() and dict() functions are used to convert the key-values list to a flat dictionary.

Implementation :

# Python3 code to illustrate the conversion
# of key-values list to flat dictionary

# Importing product module form itertools
from itertools import product

# Initializing a dictionary with key-values
test_dict = {'Word' : ['W3C', 'CS', 'W3Colleges'],
  'Character_Count' : [3, 2, 10]}

# printing the original dictionary with key-values
print("The original dictionary is : " + str(test_dict))

# Converting key-values list to flat dictionary
# Using dict() + zip() functions
res = dict(zip(test_dict['Word'], test_dict['Character_Count']))

# printing the result
print("The flattened dictionary is: " + str(res))

Output :

The original dictionary is : {'Word': ['W3C', 'CS', 'W3Colleges'], 'Character_Count': [3, 2, 10]}
The flattened dictionary is: {'W3C': 3, 'CS': 2, 'W3Colleges': 10}
Comment
Mithlesh Upadhyay Published 15 Nov 2026 · 3 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Related tutorials