Given a list, your task is to determine the list is K percent same i.e has element that is populated more than K % times.
Given below are few methods to solve the task.
Method #1 Using collections.Counter
# Python3 code to demonstrate # to check whether the list # K percent same or not from collections import Counter # initializing list ini_list1 = [1, 2, 3, 1, 1, 1, 1, 1, 3, 2] # printing initial list print ("Initial list", ini_list1) # initializing K K = 60 # code to check whether list is K % same or not i, freq = Counter(ini_list1).most_common(1)[0] if len(ini_list1)*(K / 100) <= freq: print("True") else: print("False") |
chevron_right
filter_none
Output:
Initial list [1, 2, 3, 1, 1, 1, 1, 1, 3, 2] True
Method #2: Using dictionary and its values
# Python3 code to demonstrate # to check whether the list # K percent same or not from collections import Counter, defaultdict # initializing list ini_list1 = [1, 2, 3, 1, 1, 1, 1, 1, 3, 2] # printing initial list print ("Initial list", ini_list1) # initializing K K = 60 # code to check whether list is K % same or not freq = defaultdict(int) for x in ini_list1: freq[x] += 1freq = freq.values() if max(freq) >= (K / 100) * sum(freq): print ("True") else: print ("False") |
chevron_right
filter_none
Output:
initial list [1, 2, 3, 1, 1, 1, 1, 1, 1, 1] True
Recommended Posts:
- Python | Measure similarity between two sentences using cosine similarity
- Python | Word Similarity using spaCy
- Python | Similarity metrics of strings
- Python | Percentage similarity of lists
- Measuring the Document Similarity in Python
- Measure similarity between images using Python-OpenCV
- Python - Alternate elements Similarity
- NLP | WuPalmer - WordNet Similarity
- NLP | Leacock Chordorow (LCH) and Path similarity for Synset
- Movie recommender based on plot summary using TF-IDF Vectorization and Cosine similarity
- Python | Test if string contains element from list
- Python | Test if any list element returns true for condition
- Python - Test if greater than preceding element in Tuple List
- Python | Slicing list from Kth element to last element
- Python | Insert Nth element to Kth element in other list
- Python | Replace elements in second list with index of same element in first list
- Python | Move one list element to another list
- Python | Indices list of matching element from other list
- Python | Subtract two list elements if element in first list is greater
- Python program to get the indices of each element of one list in another list
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.

