Sitemap
Dosiik1

Dosiik1

tuple

2 min readDec 8, 2021

There are 4 collection data types in py:

Lists: Is a collection which is ordered and changeable. Allows duplicate members

Tuple: is a collection which is ordered and unchangeable. Allows duplicate members

Set: is a collection which is unordered and unindexed. No duplicate members

Dictionary :is a collection which is unordered, changeable and indexed. No duplicate members.

To create a tuple with only one item, you have to add a comma after the item, otherwise Python will not recognize it as a tuple.

Possible values

type() == <class ‘tuple’>

Press enter or click to view image in full size

output: (‘orange’, ‘kiwi’, ‘melon’)

Change Tuple Values. Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called.

Get Dosiik1’s stories in your inbox

Join Medium for free to get updates from this writer.

But there is a workaround. You can convert the tuple into a list, change the list, and convert the list back into a tuple.

output: (“apple”, “kiwi”, “cherry”) changes: banana changed to kiwi

Press enter or click to view image in full size

1st way to add new items: Convert into a list

2nd way to add new items: Add tuple to a tuple

Press enter or click to view image in full size
To delete items we can use append() method, but after converting tuple to list.

To delete items we can use append() method, but after converting tuple to list.

Press enter or click to view image in full size
Also we can delete the tuple completely

Also we can delete the tuple completely

--

--