Open In App

Python – Column-wise elements in Dictionary value list

Last Updated : 27 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given dictionary with value list, extract elements columnwise.

Input : test_dict = {‘Gfg’ : [3, 6], ‘is’ : [4, 2], ‘best’ :[9, 1]} 
Output : [3, 4, 9, 6, 2, 1] 
Explanation : 3, 4, 9 from col1 and then 6, 2, 1 from 2 are extracted in order. 

Input : test_dict = {‘Gfg’ : [3], ‘is’ : [4], ‘best’ :[9]} 
Output : [3, 4, 9] 
Explanation : 3, 4, 9 from col1 in order.

Method #1 : Using generator expression + zip() + * operator

In this, we perform task of extracting columnwise using zip() and * operator is used to unpack values to be further flattened in  generator expression.

Python3




# Python3 code to demonstrate working of
# Column-wise elements in Dictionary value list
# Using generator expression + zip() + * operator
 
# initializing dictionary
test_dict = {'Gfg' : [3, 6, 7],
             'is' : [4, 2, 6],
             'best' :[9, 1, 3]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# values() gets all values at on
res = list(a for b in zip(*test_dict.values()) for a in b)
 
# printing result
print("The extracted values : " + str(res))


Output

The original dictionary is : {'Gfg': [3, 6, 7], 'is': [4, 2, 6], 'best': [9, 1, 3]}
The extracted values : [3, 4, 9, 6, 2, 1, 7, 6, 3]

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.from_iterable() + zip() + * operator

In this, task of flattening is done using chain.from_iterable(). Rest all functionalities are similar to above method.

Python3




# Python3 code to demonstrate working of
# Column-wise elements in Dictionary value list
# Using chain.from_iterable() + zip() + * operator
from itertools import chain
 
# initializing dictionary
test_dict = {'Gfg' : [3, 6, 7],
             'is' : [4, 2, 6],
             'best' :[9, 1, 3]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# values() gets all values at on
res = list(chain.from_iterable(zip(*test_dict.values())))
 
# printing result
print("The extracted values : " + str(res))


Output

The original dictionary is : {'Gfg': [3, 6, 7], 'is': [4, 2, 6], 'best': [9, 1, 3]}
The extracted values : [3, 4, 9, 6, 2, 1, 7, 6, 3]

Method #3: Using nested for loops

Step-by-step approach:

  1. Use nested for loops to access list of lists column wise
  2. Append elements to output list column wise
  3. Display output list

Python3




# Python3 code to demonstrate working of
# Column-wise elements in Dictionary value list
 
# initializing dictionary
test_dict = {'Gfg' : [3, 6, 7],
            'is' : [4, 2, 6],
            'best' :[9, 1, 3]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
x=list(test_dict.values())
res=[]
for i in range(0,len(x)):
    for j in range(0,len(x[i])):
        res.append(x[j][i])
 
 
# printing result
print("The extracted values : " + str(res))


Output

The original dictionary is : {'Gfg': [3, 6, 7], 'is': [4, 2, 6], 'best': [9, 1, 3]}
The extracted values : [3, 4, 9, 6, 2, 1, 7, 6, 3]

Time Complexity: O(M*N) M- length of values list N – length of each value (which is a list)
Auxiliary Space: O(M*N) M- length of values list N – length of each value (which is a list)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads