PY
py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
'''
Both methods fails if we have list in tuple as list cannot be hashed due to nature of mutable
In such case, how to find unique elements in tuple?
To observe failure, use below
tup = (1,2,3,2, "Hello", "Hello",[1,2])
'''
def FindCountInTuple_1(tup):
dict = {}
st = set(tup)
for elem in st:
dict[elem] = tup.count(elem)
print(dict)
def FindCountInTuple_2(tup):
dict = {}
for elem in tup:
if elem in dict:
dict[elem] += 1
else:
dict[elem] = 1
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run