How can I define duplicate items in a Python tuple?


You can directly enter duplicate items in a Python tuple as it doesn't behave like a set(which takes only unique items). 

example

myTpl = (1, 2, 2, 2, 3, 5, 5, 4)

You can also use operators on tuples to compose large tuples.For Example

myTpl = (1,) * 5
print(myTpl)

Output

This will give the output

(1,1,1,1,1)

You can also join tuples using + operator. 

example

myTpl = (1,) * 3 + (2,) * 2
print(myTpl)

Output

This will give the output

(1,1,1,2,2)

Updated on: 05-Mar-2020

591 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements