Sometimes, while working with dictionaries, we might have a problem in which we have lists as it’s value and wish to have it cumulatively in single list by concatenation. This problem can occur in web development domain. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using sum() + values()
This is the most recommended method and one liner to perform this task. In this, we access all list values using values() and concatenation utility is performed using sum().
# Python3 code to demonstrate working of # Concatenating dictionary value lists # Using sum() + values() # initializing dictionary test_dict = {"Gfg" : [4, 5], "is" : [6, 8], "best" : [10]} # printing original dictionary print("The original dictionary is : " + str(test_dict)) # Concatenating dictionary value lists # Using sum() + values() res = sum(test_dict.values(), []) # printing result print("The Concatenated list values are : " + str(res)) |
The original dictionary is : {'Gfg': [4, 5], 'best': [10], 'is': [6, 8]}
The Concatenated list values are : [4, 5, 10, 6, 8]
Method #2 : Using chain() + * operator
This task can also be performed using the combination of these methods. In these, we just use inbuilt function of chain for concatenation to list and * operator is used to access all the list values into one.
# Python3 code to demonstrate working of # Concatenating dictionary value lists # Using chain() + * operator from itertools import chain # initializing dictionary test_dict = {"Gfg" : [4, 5], "is" : [6, 8], "best" : [10]} # printing original dictionary print("The original dictionary is : " + str(test_dict)) # Concatenating dictionary value lists # Using chain() + * operator res = list(chain(*test_dict.values())) # printing result print("The Concatenated list values are : " + str(res)) |
The original dictionary is : {'Gfg': [4, 5], 'best': [10], 'is': [6, 8]}
The Concatenated list values are : [4, 5, 10, 6, 8]
Recommended Posts:
- Python | Ways to concatenate two lists
- Python | Concatenate two lists element-wise
- Python program to concatenate every elements across lists
- Python - Concatenate Dictionary string values
- Python - Concatenate String values in Dictionary List
- Python - Concatenate Tuple to Dictionary Key
- Python - Convert Lists into Similar key value lists
- Python | Convert list of tuples to dictionary value lists
- Python | Iterate through value lists dictionary
- Python - Cumulative product of dictionary value lists
- Python - Convert Key-Value list Dictionary to List of Lists
- Python - Filter odd elements from value lists in dictionary
- Python - Unique value keys in a dictionary with lists as values
- Python - Reinitialize Value lists to K in Dictionary
- Python - Slice till K dictionary value lists
- Python - Convert Strings to Uppercase in Dictionary value lists
- Python - Dictionary Key Value lists combinations
- Python - Cross mapping of Two dictionary value lists
- Python - Dictionary value lists lengths product
- Python | Pretty Print a dictionary with dictionary value
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

