Python – Value Dictionary from Record List
Last Updated :
29 Jul, 2024
Sometimes, while working with Python Records lists, we can have problems in which, we need to reform the dictionary taking just values of the binary dictionary. This can have applications in many domains which work with data. Let us discuss certain ways in which this task can be performed.
Method #1 : Using loop + values() + update()
The combination of the above functions can be used to perform this task. In this the values are extracted using values() and updating the new dictionary is done using update().
Python
# Python3 code to demonstrate working of
# Value Dictionary from Record List
# Using loop + values() + update()
# initializing list
test_list = [{1: 'gfg', 2: 'best'}, {3: 'for', 4: 'geeks'}]
# printing original list
print("The original list is : " + str(test_list))
# Value Dictionary from Record List
# Using loop + values() + update()
res = dict()
for sub in test_list:
res.update((sub.values(), ))
# printing result
print("The values dictionary is : " + str(dict(res)))
OutputThe original list is : [{1: 'gfg', 2: 'best'}, {3: 'for', 4: 'geeks'}]
The values dictionary is : {'gfg': 'best', 'for': 'geeks'}
Time Complexity: O(n) where n is the total number of values in the list “test_list”.
Auxiliary Space: O(n) where n is the total number of values in the list “test_list”.
Method #2 : Using zip() + iter()
The combination of the above functions can also be used to perform this task. In this, we perform to convert the list to iterator and pairing of values is done using zip().
Python
# Python3 code to demonstrate working of
# Value Dictionary from Record List
# Using zip() + iter()
# initializing list
test_list = [{1: 'gfg', 2: 'best'}, {3: 'for', 4: 'geeks'}]
# printing original list
print("The original list is : " + str(test_list))
# Value Dictionary from Record List
# Using zip() + iter()
res = dict()
for sub in test_list:
itr = iter(sub.values())
res.update(dict(zip(itr, itr)))
# printing result
print("The values dictionary is : " + str(res))
OutputThe original list is : [{1: 'gfg', 2: 'best'}, {3: 'for', 4: 'geeks'}]
The values dictionary is : {'gfg': 'best', 'for': 'geeks'}
Time Complexity: O(n) where n is the number of elements in the dictionary. The zip() + iter() is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the dictionary.
Method #3:Using List comprehension
Algorithm
- Initialize an empty dictionary res.
- Iterate over each dictionary sub in the record list test_list.
- Get the values of the dictionary sub using the values() method, and update the res dictionary with these values using the update() method.
- Convert the resulting res dictionary to a standard dictionary using the dict() function.
- Print the resulting dictionary.
Python
# initializing list
test_list = [{1 : 'gfg', 2 : 'best'}, {3 : 'for', 4 : 'geeks'}]
# printing original list
print("The original list is : " + str(test_list))
# Value Dictionary from Record List
# Using list comprehension + values() + update()
res = dict()
[res.update((sub.values(), )) for sub in test_list]
# printing result
print("The values dictionary is : " + str(dict(res)))
OutputThe original list is : [{1: 'gfg', 2: 'best'}, {3: 'for', 4: 'geeks'}]
The values dictionary is : {'gfg': 'best', 'for': 'geeks'}
Time Complexity: O(N*M)
The loop iterates over each dictionary in the record list once. Therefore, the time complexity of this algorithm is O(N*M), where N is the number of dictionaries in the list and M is the average size of each dictionary.
Auxiliary Space: O(N*M)
The space complexity of this algorithm is O(NM), where N is the number of dictionaries in the list and M is the average size of each dictionary. The res dictionary stores all the values from each dictionary in the list, which can take up NM space. Additionally, other variables and temporary memory used during the execution of the algorithm can also contribute to space complexity.
Method #4: Using for loop+values() method
Approach:
- Create an empty dictionary.
- Initiate a for loop to traverse list of dictionaries.
- Extract values of each dictionary using values() method.
- Make 0th index value of values list as key and 1st index value of values list as value.
- Display the dictionary.
Below is the implementation of the above approach:
Python
# Python3 code to demonstrate working of
# Value Dictionary from Record List
# initializing list
test_list = [{1: 'gfg', 2: 'best'}, {3: 'for', 4: 'geeks'}]
# printing original list
print("The original list is : " + str(test_list))
# Value Dictionary from Record List
res = dict()
for i in test_list:
x = list(i.values())
res[x[0]] = x[1]
# printing result
print("The values dictionary is : " + str(dict(res)))
OutputThe original list is : [{1: 'gfg', 2: 'best'}, {3: 'for', 4: 'geeks'}]
The values dictionary is : {'gfg': 'best', 'for': 'geeks'}
Time Complexity: O(M*N), where M is the length of list of dictionaries and N is the length of each dictionary
Auxiliary Space: O(M*N), where M is the number of keys and N is the number of values
Method #5: Using dictionary comprehension
Python
# Python3 code to demonstrate working of
# Value Dictionary from Record List
# initializing list
test_list = [{1: 'gfg', 2: 'best'}, {3: 'for', 4: 'geeks'}]
# printing original list
print("The original list is : " + str(test_list))
# Value Dictionary from Record List
res = {list(i.values())[0]: list(i.values())[1] for i in test_list}
# printing result
print("The values dictionary is : " + str(dict(res)))
OutputThe original list is : [{1: 'gfg', 2: 'best'}, {3: 'for', 4: 'geeks'}]
The values dictionary is : {'gfg': 'best', 'for': 'geeks'}
Time complexity: O(n), where n is the length of test_list.
Auxiliary space: O(n)
Method #6: Using map() and lambda
Python
# Python3 code to demonstrate working of
# Value Dictionary from Record List
# initializing list
test_list = [{1: 'gfg', 2: 'best'}, {3: 'for', 4: 'geeks'}]
# printing original list
print("The original list is : " + str(test_list))
# Value Dictionary from Record List using map() and lambda
res = dict(map(lambda x: (list(x.values())[0], list(x.values())[1]), test_list))
# printing result
print("The values dictionary is : " + str(res))
OutputThe original list is : [{1: 'gfg', 2: 'best'}, {3: 'for', 4: 'geeks'}]
The values dictionary is : {'gfg': 'best', 'for': 'geeks'}
Time complexity: O(n), where n is the length of test_list.
Auxiliary space: O(n)
Similar Reads
Python Remove Item from Dictionary by Value
We are given a dictionary and our task is to remove key-value pairs where the value matches a specified target. This can be done using various approaches, such as dictionary comprehension or iterating through the dictionary. For example: d = {"a": 10, "b": 20, "c": 10, "d": 30} and we have to remove
3 min read
Python - Remove Item from Dictionary
There are situations where we might want to remove a specific key-value pair from a dictionary. For example, consider the dictionary d = {'x': 10, 'y': 20, 'z': 30}. If we need to remove the key 'y', there are multiple ways to achieve this. Let's discuss several methods to remove an item from a dict
3 min read
Set from Dictionary Values - Python
The task is to extract unique values from a dictionary and convert them into a set. In Python, sets are unordered collections that automatically eliminate duplicates. The goal is to extract all the values from the dictionary and store them in a set. For example, given a dictionary like d = {'A': 4,
3 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 - Values from custom List in Records
Sometimes, while working with Python records, we can have a problem in which we need to extract all the values from the custom list in list dictionary records. This problem can have applications in domains such as web development and school programming. Let's discuss certain ways in which this task
7 min read
Python | Iterate through value lists dictionary
While working with dictionary, we can have a case in which we need to iterate through the lists, which are in the keys of dictionaries. This kind of problem can occur in web development domain. Let's discuss certain ways in which this problem can be solved. Method #1: Using list comprehension List c
4 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 - 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
Remove Kth Key from Dictionary - Python
We are given a dictionary we need to remove Kth key from the dictionary. For example, we are given a dictionary d = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4'} we need to remove the key2 so that the output should be {'key1': 'value1', 'key3': 'value3', 'key4': 'value4'}.
3 min read
Updating Value List in Dictionary - Python
We are given a dictionary where the values are lists and our task is to update these lists, this can happen when adding new elements to the lists or modifying existing values in them. For example, if we have a dictionary with a list as a value like this: {'gfg' : [1, 5, 6], 'is' : 2, 'best' : 3} the
4 min read