Python | Concatenate dictionary value lists
Last Updated :
27 Apr, 2023
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
# 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))
Output : The original dictionary is : {'Gfg': [4, 5], 'best': [10], 'is': [6, 8]}
The Concatenated list values are : [4, 5, 10, 6, 8]
Time Complexity: O(n), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
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
# 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))
Output : The original dictionary is : {'Gfg': [4, 5], 'best': [10], 'is': [6, 8]}
The Concatenated list values are : [4, 5, 10, 6, 8]
Method #3 : Using extend(),list() and values() methods
Python3
# Python3 code to demonstrate working of
# Concatenating dictionary value lists
# 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
x=[]
for i in list(test_dict.values()):
x.extend(i)
# printing result
print("The Concatenated list values are : " + str(x))
OutputThe original dictionary is : {'Gfg': [4, 5], 'is': [6, 8], 'best': [10]}
The Concatenated list values are : [4, 5, 6, 8, 10]
Method #4 : Using reduce() method
Python3
# Python3 code to demonstrate working of
# Concatenating dictionary value lists
# Using reduce() and values()
# importing reduce from functools
from functools import reduce
# 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 reduce() and values()
res = reduce(lambda x,y: x+y, test_dict.values())
# printing result
print("The Concatenated list values are : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe original dictionary is : {'Gfg': [4, 5], 'is': [6, 8], 'best': [10]}
The Concatenated list values are : [4, 5, 6, 8, 10]
In this example, We've used the reduce() function to iterate over the values of the dictionary and to concatenate them together. The lambda function defines how the lists are being concatenated together.
The time complexity of this approach would be O(n) where n is the number of items in the dictionary and space complexity would be O(n) as well, as we are creating a new list with all the items from the dictionary values.
Method #5 : Using nested for loop and append() method
Approach
- Access values list using values() method
- Initiate a nested for loop to append each element of list within values list to output list using append()
- Display output list
Python3
# Python3 code to demonstrate working of
# Concatenating dictionary value lists
# 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
res = []
x=list(test_dict.values())
for i in x:
for j in i:
res.append(j)
# printing result
print("The Concatenated list values are : " + str(res))
OutputThe original dictionary is : {'Gfg': [4, 5], 'is': [6, 8], 'best': [10]}
The Concatenated list values are : [4, 5, 6, 8, 10]
Time Complexity : O(M*N) M- length of values list N - length of each list in value list
Auxiliary Space : O(M*N) M- length of values list N - length of each list in value list
Similar Reads
Python - Concatenate String values in Dictionary List
Sometimes, while working with Python records data, we can have a problem in which we require to perform concatenation of string values of keys by matching at particular key like ID. This kind of problem can have application in web development domain. Let's discuss certain way in which this task can
4 min read
Python - Concatenate Dictionary string values
The task of concatenating string values in a Python dictionary involves combining the values associated with the same key across multiple dictionaries. This operation allows us to merge string data in an efficient manner, especially when dealing with large datasets .For example, given two dictionari
4 min read
Python - Dictionary Key Value lists combinations
Given a dictionary with values as a list, extract all the possible combinations, both cross keys and with values. Input : test_dict = {"Gfg" : [4, 5], "is" : [1, 2], "Best" : [9, 4]} Output : {0: [['Gfg', 4], ['is', 1], ['Best', 9]], 1: [['Gfg', 4], ['is', 1], ['Best', 4]], 2: [['Gfg', 4], ['is', 2]
9 min read
Python Convert Dictionary to List of Values
Python has different types of built-in data structures to manage your data. A list is a collection of ordered items, whereas a dictionary is a key-value pair data. Both of them are unique in their own way. In this article, the dictionary is converted into a list of values in Python using various con
3 min read
Python - Concatenate Tuple to Dictionary Key
Given Tuples, convert them to the dictionary with key being concatenated string. Input : test_list = [(("gfg", "is", "best"), 10), (("gfg", "for", "cs"), 15)] Output : {'gfg is best': 10, 'gfg for cs': 15} Explanation : Tuple strings concatenated as strings. Input : test_list = [(("gfg", "is", "best
6 min read
Python - Convert dictionary items to values
Sometimes, while working with Python dictionary, we can have a problem in which we need to convert all the items of dictionary to a separate value dictionary. This problem can occur in applications in which we receive dictionary in which both keys and values need to be mapped as separate values. Let
3 min read
Convert Matrix to Dictionary Value List - Python
We are given a matrix and the task is to map each column of a matrix to customized keys from a list. For example, given a matrix li = [[4, 5, 6], [1, 3, 5], [3, 8, 1], [10, 3, 5]] and a list map_li = [4, 5, 6], the goal is to map the first column to the key 4, the second column to the key 5, and the
3 min read
Python - Common items Dictionary Value List
The functionality of union has been discussed many times. But sometimes, we can have a more complex container, in which we need to check for the intersection of lists which are in form of keys of dictionary. Letâs discuss certain ways to solve this type of problem. Method #1: Using Loops Using loops
10 min read
Convert Dictionary Value list to Dictionary List Python
Sometimes, while working with Python Dictionaries, we can have a problem in which we need to convert dictionary list to nested records dictionary taking each index of dictionary list value and flattening it. This kind of problem can have application in many domains. Let's discuss certain ways in whi
9 min read
Python - Convert key-values list to flat dictionary
We are given a list that contains tuples with the pairs of key and values we need to convert that list into a flat dictionary. For example a = [("name", "Ak"), ("age", 25), ("city", "NYC")] is a list we need to convert it to dictionary so that output should be a flat dictionary {'name': 'Ak', 'age':
3 min read