Program to assign the frequency of tuples to each tuple

In this python tuple program, we will assign the frequency of tuples to each tuple.

Steps to solve the program
  1. Take a list of tuples as input and create an empty dictionary.
  2. Use for loop to iterate over each tuple from the list.
  3. If a tuple is not in the dictionary then add that tuple as a key and 0 as its value.
  4. If the tuple is already in the list then add 1 to the value.
  5. Print the output.
				
					l = [('s','q'),('t','o','o','l'),('p','y'),('s','q')]
print("Original list of tuples: ",l)
d = {}
for tup in l:
    if tup not in d:
        d[tup] = 0
    if tup in d:
        d[tup] += 1
print("Frequency count : ",d)
				
			

Output :

				
					Original list of tuples:  [('s', 'q'), ('t', 'o', 'o', 'l'), ('p', 'y'), ('s', 'q')]
Frequency count :  {('s', 'q'): 2, ('t', 'o', 'o', 'l'): 1, ('p', 'y'): 1}
				
			

filter out tuples that have more than 3 elements.

find values of tuples at ith index number.

Leave a Comment