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

Image of what i tried

Harsh Mittal
  • 39
  • 1
  • 1
  • 8

4 Answers4

2
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)
Harsh Mittal
  • 39
  • 1
  • 1
  • 8
0

You can use a collections.Counter:

from collections import Counter

for k, v in Counter(tup).items():
    if v > 1:
        print("Repeated: {}".format(k))
user2390182
  • 72,016
  • 6
  • 67
  • 89
  • --------------------------------------------------------------------------- TypeError Traceback (most recent call last) in 4 for k, v in Counter(tup).items(): 5 if v > 1: ----> 6 print("Repeated: " + k) TypeError: can only concatenate str (not "int") to str – Harsh Mittal Feb 04 '21 at 15:34
0
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

0

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.