Friday, October 17, 2025
HomeLanguagesPython – Storing Elements Greater than K as Dictionary

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 list
test_list = [12, 44, 56, 34, 67, 98, 34]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing K
K = 50
 
# Storing Elements Greater than K as Dictionary
# using loop
res = dict()
count = 1
for ele in test_list:
    if ele > K:
        res[count] = ele
        count = count + 1
         
# printing result
print ("The dictionary after storing elements : " + str(res))


Output : 

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 list
test_list = [12, 44, 56, 34, 67, 98, 34]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing K
K = 50
 
# Storing Elements Greater than K as Dictionary
# using dictionary comprehension
res = {idx: ele for idx, ele in enumerate(test_list) if ele >= K}
         
# printing result
print ("The dictionary after storing elements : " + str(res))


Output : 

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 list
test_list = [12, 44, 56, 34, 67, 98, 34]
   
# printing original list
print("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))


Output

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)

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS