Python – First K unique elements

Sometimes, while working with Python Lists, we can have a problem in which we need to extract first K unique elements. This means we need to extract duplicate if they occur in first K elements as well. This can essentially make count of first K unique elements more than K. This kind of problem can have application in day-day programming. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [6, 7, 6, 7], K = 2
Output : [6, 7]

Input : test_list = [3, 4, 5, 7], K = 3
Output : [3, 4, 5]

Method #1 : Using loop
This is brute force way in which this task can be performed. In this, we perform task of getting elements by keeping counter and store list to compare previous occurrences.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# First K unique elements
# Using loop
  
# initializing list
test_list = [6, 7, 6, 7, 8, 3, 9, 11]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing K 
K = 4
  
# First K unique elements
# Using loop
store = []
res = []
cnt = 0
for ele in test_list:
    if ele not in store:
        cnt = cnt + 1
        store.append(ele)
    res.append(ele)
    if cnt >= K :
        break
          
# printing result 
print("The extracted elements : " + str(res)) 

chevron_right


Output :



The original list is : [6, 7, 6, 7, 8, 3, 9, 11]
The extracted elements : [6, 7, 6, 7, 8, 3]

 

Method #2 : Using set() + filter() + lambda
The combination of above functions can be used to solve this problem. In this, we perform the task of creating lookup list using set and filter() + lambda is used to check the values against list.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# First K unique elements
# Using set() + filter() + lambda
  
# initializing list
test_list = [6, 7, 6, 7, 8, 3, 9, 11]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing K 
K = 4
  
# First K unique elements
# Using set() + filter() + lambda
store = set(list({ele for ele in test_list})[:K])
res = list(filter(lambda ele: ele in store, test_list))
          
# printing result 
print("The extracted elements : " + str(res)) 

chevron_right


Output :

The original list is : [6, 7, 6, 7, 8, 3, 9, 11]
The extracted elements : [6, 7, 6, 7, 8, 3]



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.