'How to find the repeated items of a tuple in python

tup=(1,3,4,32,1,1,1)  
for i in tup:
    if tup.count(i) > 1:
        print('REPEATED')

Image of what i tried



Solution 1:[1]

tup=(1,3,4,32,1,1,1,31,32,12,21,2,3)  
for i in tup:
    if tup.count(i) > 1:
        print(i)

Solution 2:[2]

You can use a collections.Counter:

from collections import Counter

for k, v in Counter(tup).items():
    if v > 1:
        print("Repeated: {}".format(k))

Solution 3:[3]

var=int(input())
tup=(10, 8, 5, 2, 10, 15, 10, 8, 5, 8, 8, 2)  
a=list(tup)
for i in range(len(a)):
  a[i]=int(a[i])
count=a.count(var)
print(var,'appears',count,'times in the tuple')

Sample Input

8

Sample Output

8 appears 4 times in the tuple

Solution 4:[4]

The Pythonic way to do this is to use collections.Counter:

>>> from collections import Counter
>>> t = (1,1,1,1,1,2,2,2,3,3,4,5,6,7,8,9)
>>> c = Counter(t)
>>> c
Counter({1: 5, 2: 3, 3: 2, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1})

Counter returns a dict-like object that can be iterated through, along with other operations.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Harsh Mittal
Solution 2
Solution 3 Rafid Khan
Solution 4 Matt Minton