3 Python List, Tuple, Set 05-10-2022
3 Python List, Tuple, Set 05-10-2022
Lists are enclosed in brackets ( [ ] ) and their elements and size can be
changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be
updated.
Example
Tuples allow duplicate values:
Example
One item tuple, remember the comma:
thistuple = ("apple",)
print(type(thistuple))
#NOT a tuple
thistuple = ("apple")
print(type(thistuple))
Tuple Items - Data Types
Tuple items can be of any data type:
Example
String, int and boolean data types:
Example
A tuple with strings, integers and boolean
values:
Example
Print the second item in the tuple:
Example
Check if "apple" is present in the tuple:
Example
Print the last item of the tuple:
Example
Return the third, fourth, and fifth item:
thistuple =
("apple", "banana", "cherry", "orange", "kiwi", "mel
on", "mango")
print(thistuple[2:5])
indexing
thistuple =
("apple", "banana", "cherry", "orange",
"kiwi", "melon", "mango")
print(thistuple[:4])
thistuple =
("apple", "banana", "cherry", "orange", "
kiwi", "melon", "mango")
print(thistuple[2:])
Python - Update Tuples
Change Tuple Values
Example
Convert the tuple into a list to be able to change it:
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)
Add Items
Since tuples are immutable, they do not have a build-in append()
method, but there are other ways to add items to a tuple.
Example
Convert the tuple into a list, add "orange", and convert it back into
a tuple:
thistuple = ("apple", "banana", "cherry")
y = list(thistuple)
y.append("orange")
thistuple = tuple(y)
Add tuple to a tuple. You are allowed to add tuples to
tuples, so if you want to add one item, (or many), create
a new tuple with the item(s), and add it to the existing
tuple:
Example
Create a new tuple with the value "orange", and add that
tuple:
print(thistuple)
Remove Items
Tuples are unchangeable, so you cannot remove items
from it, but you can use the same workaround as we used
for changing and adding tuple items:
Example
Convert the tuple into a list, remove "apple", and convert
it back into a tuple:
Unpacking a tuple:
fruits = ("apple", "banana", "cherry")
print(green)
print(yellow)
print(red)
Packing a tuple:
fruits = ("apple", "banana", "cherry")
Join Two Tuples
To join two or more tuples you can use the +
operator:
Example
Join two tuples:
Example
Multiply the fruits tuple by 2:
print(mytuple)
Python - Tuple Methods
Tuple Methods
Python has two built-in methods that you can use on tuples.
Method Description
count() Returns the number of times a
specified value occurs in a tuple
index() Searches the tuple for a specified
value and returns the position of where it was
found
Change Tuple Values
Once a tuple is created, you cannot change its values.
Tuples are unchangeable, or immutable as it also is called.
Example
Convert the tuple into a list to be able to change it:
Example
Convert the tuple into a list, remove "apple", and convert
it back into a tuple:
print(green)
print(yellow)
print(red)