2 Python: Repeated items of a tuple - w3resource
w3resource

Python Exercise: Repeated items of a tuple


9. Find Repeated Items in a Tuple

Write a Python program to find repeated items in a tuple.

Visual Presentation:

Python Tuple: Repeated items of a tuple.

Sample Solution:

Python Code:

# Create a tuple containing a sequence of numbers
tuplex = 2, 4, 5, 6, 2, 3, 4, 4, 7
# Print the contents of the 'tuplex' tuple
print(tuplex)

# Count the number of times the value 4 appears in the 'tuplex' tuple
count = tuplex.count(4)
# Print the count of how many times 4 appears
print(count) 

Sample Output:

(2, 4, 5, 6, 2, 3, 4, 4, 7)                                                                                   
3   

Flowchart:

Flowchart: Repeated items of a tuple

For more Practice: Solve these Related Problems:

  • Write a Python program to identify all items in a tuple that appear more than once using a frequency dictionary.
  • Write a Python program to return a set of repeated elements from a tuple using list comprehension.
  • Write a Python program to iterate over a tuple and print each element that occurs multiple times.
  • Write a Python program to use collections.Counter to find and output all repeated items in a tuple.

Python Code Editor:

Previous: Write a Python program to create the colon of a tuple.
Next: Write a Python program to check whether an element exists within a tuple.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.