Python | Concatenate dictionary value lists
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 | Iterate through value lists dictionary
- Python | Convert two lists into a dictionary
- Python | Ways to create a dictionary of Lists
- Python | Initializing dictionary with empty lists
- Python | Convert list of tuples to dictionary value lists
- Python | Split dictionary of lists to list of dictionaries
- Python | Split dictionary keys and values into separate lists
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Create pandas dataframe from lists using dictionary
- Python | Convert string dictionary to dictionary
- Python | Pretty Print a dictionary with dictionary value
- Python | Ways to concatenate boolean to string
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.



