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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output :

The original list is : [12, 44, 56, 34, 67, 98, 34]
The dictionary after storing elements : {1: 56, 2: 67, 3: 98}

 

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output :

The original list is : [12, 44, 56, 34, 67, 98, 34]
The dictionary after storing elements : {2: 56, 4: 67, 5: 98}



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.