Python – Check if Kth index elements are unique

Given a String list, check if all Kth index elements are unique.

Input : test_list = [“gfg”, “best”, “for”, “geeks”], K = 1
Output : False
Explanation : e occurs as 1st index in both best and geeks.

Input : test_list = [“gfg”, “best”, “geeks”], K = 2
Output : True
Explanation : g, s, e, all are unique.

Method #1 : Using loop

This is brute way to solve this problem. In this, we iterate for each string and flag off when any element is repeated, and return false.



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Check if Kth index elements are unique
# Using loop
  
# initializing list
test_list = ["gfg", "best", "for", "geeks"
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing K 
K = 2
  
res = []
flag = True
for ele in test_list:
      
    # checking if element is repeated
    if ele[K] in res:
        flag = False
        break
    else:
        res.append(ele[K])
          
# printing result 
print("Is Kth index all unique : " + str(flag))

chevron_right


Output

The original list is : ['gfg', 'best', 'for', 'geeks']
Is Kth index all unique : True

Method #2 : Using Counter() + all()

In this, we count frequency of each char. at Kth index, and all() is used to check if Counter is less than 2 for all.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Check if Kth index elements are unique
# Using Counter() + all()
from collections import Counter
  
# initializing list
test_list = ["gfg", "best", "for", "geeks"
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing K 
K = 2
  
# getting count of each Kth index item
count = Counter(sub[K] for sub in test_list)
  
# extracting result 
res = all(val < 2 for val in count.values())
          
# printing result 
print("Is Kth index all unique : " + str(res))

chevron_right


Output

The original list is : ['gfg', 'best', 'for', 'geeks']
Is Kth index all unique : True




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.