Python – Storing Elements Greater than K as Dictionary

Sometimes, while working with python lists, we can have a problem in which we need to extract elements greater than K. But sometimes, we don’t require to store duplicacy and hence store by key value pair in dictionary. To track of number position of occurrence in dictionary.
Method #1 : Using loop This is brute force way in which this task can be performed. In this, we store elements in form of dictionary by checking for elements greater than K.
Python3
# Python3 code to demonstrate # Storing Elements Greater than K as Dictionary# using loop# Initializing listtest_list = [12, 44, 56, 34, 67, 98, 34]# printing original listprint("The original list is : " + str(test_list))# Initializing K K = 50# Storing Elements Greater than K as Dictionary# using loopres = dict()count = 1for ele in test_list: if ele > K: res[count] = ele count = count + 1 # printing result print ("The dictionary after storing elements : " + str(res)) |
The original list is : [12, 44, 56, 34, 67, 98, 34]
The dictionary after storing elements : {1: 56, 2: 67, 3: 98}
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.
Method #2 : Using dictionary comprehension This is yet another way in which this task can be performed. In this, we just perform similar task in a shorted construct using dictionary comprehension and enumerate() for indexing.
Python3
# Python3 code to demonstrate # Storing Elements Greater than K as Dictionary# using dictionary comprehension# Initializing listtest_list = [12, 44, 56, 34, 67, 98, 34]# printing original listprint("The original list is : " + str(test_list))# Initializing K K = 50# Storing Elements Greater than K as Dictionary# using dictionary comprehensionres = {idx: ele for idx, ele in enumerate(test_list) if ele >= K} # printing result print ("The dictionary after storing elements : " + str(res)) |
The original list is : [12, 44, 56, 34, 67, 98, 34]
The dictionary after storing elements : {2: 56, 4: 67, 5: 98}
Time Complexity: O(n), where n is the length of the dictionary
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res dictionary
Method #3 : Using filter()
This is yet another approach to perform this task, in this we just use filter() to filter out all the elements which are greater than K and then use dict() to convert the result into dictionary.
Python3
# Python3 code to demonstrate # Storing Elements Greater than K as Dictionary# using filter() # Initializing listtest_list = [12, 44, 56, 34, 67, 98, 34] # printing original listprint("The original list is : " + str(test_list)) # Initializing K K = 50 # Storing Elements Greater than K as Dictionary# using filter()res = dict(filter(lambda x : x[1] >= K, enumerate(test_list))) # printing result print ("The dictionary after storing elements : " + str(res)) |
The original list is : [12, 44, 56, 34, 67, 98, 34]
The dictionary after storing elements : {2: 56, 4: 67, 5: 98}
Time complexity : O(N)
Auxiliary Space : O(N)



