Python | Grouping list values into dictionary
Last Updated :
18 May, 2023
Sometimes, while working with data, we can be encountered a situation in which we have a list of lists and we need to group its 2nd index with the common initial element in lists. Let’s discuss ways in which this problem can be solved.
Method 1: Using defaultdict() + loop + dict()
The defaultdict can be used to initialize the group elements and the loop can be used to group the values together and conversion to a dictionary can be done using dict().
Python3
from collections import defaultdict
test_list = [[ 'Gfg' , 1 ], [ 'Gfg' , 2 ], [ 'is' , 3 ], [ 'best' , 4 ], [ 'is' , 5 ]]
print ( "The original list is : " + str (test_list))
temp = defaultdict( list )
for key, val in test_list:
temp[key].append(val)
res = dict ((key, tuple (val)) for key, val in temp.items())
print ( "The grouped dictionary is : " + str (res))
|
Output :
The original list is : [['Gfg', 1], ['Gfg', 2], ['is', 3], ['best', 4], ['is', 5]]
The grouped dictionary is : {'Gfg': (1, 2), 'best': (4, ), 'is': (3, 5)}
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list.
Method 2: Using for loops, in and not in operators
Python3
test_list = [[ 'Gfg' , 1 ], [ 'Gfg' , 2 ], [ 'is' , 3 ], [ 'best' , 4 ], [ 'is' , 5 ]]
print ( "The original list is : " + str (test_list))
x = []
for i in test_list:
if i[ 0 ] not in x:
x.append(i[ 0 ])
res = dict ()
for i in x:
a = []
for j in test_list:
if (j[ 0 ] = = i):
a.append(j[ 1 ])
res[i] = tuple (a)
print ( "The grouped dictionary is : " + str (res))
|
Output
The original list is : [['Gfg', 1], ['Gfg', 2], ['is', 3], ['best', 4], ['is', 5]]
The grouped dictionary is : {'Gfg': (1, 2), 'is': (3, 5), 'best': (4,)}
Time complexity: O(N^2) because the algorithm contains two nested loops,
Auxiliary space: O(N) because the algorithm uses a list x to store the unique elements in the first column of the input list.
Method 3: Using dictionary comprehension and setdefault()
- Initialize an empty dictionary res then loop through each element of test_list.
- For each element, get the key and value using unpacking then use the setdefault() method of dictionary to get the value for the key.
- If the key is not present in the dictionary, it initializes an empty list as the value then append the value to this list.
Example:
Python3
test_list = [[ 'Gfg' , 1 ], [ 'Gfg' , 2 ], [ 'is' , 3 ], [ 'best' , 4 ], [ 'is' , 5 ]]
res = {}
for key, value in test_list:
res.setdefault(key, []).append(value)
print ( "The grouped dictionary is : " + str (res))
|
Output
The grouped dictionary is : {'Gfg': [1, 2], 'is': [3, 5], 'best': [4]}
Time complexity: O(n), where n is the number of elements in the input list.
Auxiliary space: O(n). This is because we need to store each key-value pair of the resulting dictionary. In the worst case, if all the keys are unique, the size of the dictionary will be the same as the size of the input list.
Method 4: Using all() function and a generator expression
- Use the all() function along with a generator expression to check if all the values in the Kth index of the dictionary values are equal to N.
- The generator expression iterates over the dictionary values and checks if the Kth index of each value is equal to N.
- The all() function returns True if all the values returned by the generator expression are True, otherwise it returns False.
Example:
Python3
test_dict = { 'Gfg' : [ 1 , 4 , 8 ], 'is' : [ 8 , 4 , 2 ], 'best' : [ 7 , 4 , 9 ]}
print ( "The original dictionary : " + str (test_dict))
K = 2
N = 4
res = all (N = = val[K - 1 ] for val in test_dict.values())
print ( "Are all Kth index equal to N : " + str (res))
|
Output
The original dictionary : {'Gfg': [1, 4, 8], 'is': [8, 4, 2], 'best': [7, 4, 9]}
Are all Kth index equal to N : True
Time complexity: O(N), where N is the total number of values in the input dictionary.
Auxiliary space: O(1), as it only uses a constant amount of extra space to store the variables K, N, and res.
Method 5: using the any() function and a list comprehension.
Approach:
- Initialize the dictionary test_dict with keys and values.
- Initialize the variable K with the index of the element you want to check in each list.
- Initialize the variable N with the value you want to check if it’s present in the Kth index of each list.
- Use a list comprehension to iterate over the values in test_dict and check if N is present at the K-1th index of each list.
- Use the any() function to check if at least one of the values in the list comprehension is True. If it is, then at least one of the lists has N at the Kth index.
- Print the result.
Python3
test_dict = { 'Gfg' : [ 1 , 4 , 8 ], 'is' : [ 8 , 4 , 2 ], 'best' : [ 7 , 4 , 9 ]}
print ( "The original dictionary : " + str (test_dict))
K = 2
N = 4
res = any (N = = val[K - 1 ] for val in test_dict.values())
print ( "Is at least one Kth index equal to N : " + str (res))
|
Output
The original dictionary : {'Gfg': [1, 4, 8], 'is': [8, 4, 2], 'best': [7, 4, 9]}
Is at least one Kth index equal to N : True
Time complexity: O(N) because we need to iterate over all the values in the dictionary.
Auxiliary space: O(1) because we only need to store the values of K, N, and res.
METHOD 6:Using itertools
APPROACH:
This code takes an input list of lists and groups its values into a dictionary with the first element of each inner list as the key and the rest of the elements as the corresponding values. It does this using the itertools.groupby() and map() functions.
ALGORITHM:
1. The itertools.groupby() function is used to group the elements of the original_list by the first element of each inner list.
2. The resulting groups are then sorted using the sorted() function to ensure that the keys in the resulting dictionary are in alphabetical order.
3. The map() function is used to create a new dictionary by iterating over each group and mapping the first element of each group as the key and the second element of each group as the value.
4. Finally, the resulting dictionary is printed to the console.
Python3
import itertools
original_list = [[ 'Gfg' , 1 ], [ 'Gfg' , 2 ], [ 'is' , 3 ], [ 'best' , 4 ], [ 'is' , 5 ]]
grouped_dict = dict ( map ( lambda x: (x[ 0 ], tuple ( map ( lambda y: y[ 1 ], x[ 1 ]))),
itertools.groupby( sorted (original_list), lambda z: z[ 0 ])))
print ( "The original list is :" , original_list)
print ( "The grouped dictionary is :" , grouped_dict)
|
Output
The original list is : [['Gfg', 1], ['Gfg', 2], ['is', 3], ['best', 4], ['is', 5]]
The grouped dictionary is : {'Gfg': (1, 2), 'best': (4,), 'is': (3, 5)}
Time complexity:
The time complexity of this code is O(nlogn), where n is the length of the input list. This is because the sorted() function used to sort the groups has a time complexity of O(nlogn), and the groupby() function has a time complexity of O(n).
Space complexity:
The space complexity of this code is O(n), where n is the length of the input list. This is because the groupby() function creates an iterator object that generates the groups on-the-fly, so the amount of memory used is proportional to the number of groups, which is at most the length of the input list. The resulting dictionary also takes up space proportional to the length of the input list.
Similar Reads
Python - Summation Grouping in Dictionary List
Sometimes, while working with Python Dictionaries, we can have a problem in which we need to perform the grouping of dictionaries according to specific key, and perform summation of certain keys while grouping similar key's value. This is s peculiar problem but can have applications in domains such
5 min read
Python | Grouping dictionary keys by value
While performing computations over dictionary, we can come across a problem in which we might have to perform the task of grouping keys according to value, i.e create a list of keys, it is value of. This can other in cases of organising data in case of machine learning. Let's discuss certain way in
4 min read
Get List of Values From Dictionary - Python
We are given a dictionary and our task is to extract all the values from it and store them in a list. For example, if the dictionary is d = {'a': 1, 'b': 2, 'c': 3}, then the output would be [1, 2, 3]. Using dict.values()We can use dict.values() along with the list() function to get the list. Here,
2 min read
Python - Group Similar items to Dictionary Values List
We are given a list of items and our task is to group similar elements together as dictionary values. The keys will be the unique items and their values will be lists containing all occurrences of that item. For example, given ['apple', 'banana', 'apple', 'orange', 'banana'], the output should be: {
2 min read
Inverse Dictionary Values List - Python
We are given a dictionary and the task is to create a new dictionary where each element of the value lists becomes a key and the original keys are grouped as lists of values for these new keys.For example: dict = {1: [2, 3], 2: [3], 3: [1]} then output will be {2: [1], 3: [1, 2], 1: [3]} Using defau
2 min read
Python - Group single item dictionaries into List values
Given a List of single-item dictionaries, group them into dictionary value lists according to similar values. Input : [{"Gfg" : 3}, {"is": 8}, {"Gfg": 18}, {"Best": 33}]Output : {'Gfg': [3, 18], 'is': [8], 'Best': [33]} Explanation : Each key converted to list values and dictionary. Input : [{"Gfg"
6 min read
Python - Dictionary List Values Frequency
Sometimes, while working with Python dictionaries, we can have a problem in which we need to perform the task of computing frequency of all the values in dictionary values lists. This is quite common problem and can have use cases in many domains. Let's discuss certain ways in which this task can be
6 min read
Get Index of Values in Python Dictionary
Dictionary values are lists and we might need to determine the position (or index) of each element within those lists. Since dictionaries themselves are unordered (prior to Python 3.7) or ordered based on insertion order (in Python 3.7+), the concept of "index" applies to the valuesâspecifically whe
3 min read
Python - Group similar value list to dictionary
Sometimes, while working with Python list, we can have a problem in which we need to group similar value lists indices to values into a dictionary. This can have a good application in domains in which we need grouped dictionary as output for pair of lists. Lets discuss certain ways in which this tas
6 min read
Python - Print dictionary of list values
In this article, we will explore various ways on How to Print Dictionary in Python of list values. A dictionary of list values means a dictionary contains values as a list of dictionaries in Python. Example: {'key1': [{'key1': value,......,'key n': value}........{'key1': value,......,'key n': value}
4 min read