Create a tuple

tuple1 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
print(tuple1)
print(type(tuple1))

Create an empty tuple

tuple1 = ()
print(tuple1)
print(type(tuple1))
Output:
()
<class 'tuple'>

Access an element in a tuple

tuple1 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
print(tuple1[0])
Output:
1

Access an element in a tuple using negative index

tuple1 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
print(tuple1[-1])
Output:
10

Access a range of elements in a tuple

tuple1 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
print(tuple1[0:5])
Output:
[1, 2, 3, 4, 5]

Add an element to a tuple

tuple1 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
tuple1.append(11)
print(tuple1)
Output:
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)

Add an element to a tuple at a specific index

tuple1 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
tuple1.insert(0, 0)
print(tuple1)
Output:
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

Remove an element from a tuple

tuple1 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
tuple1.remove(10)
print(tuple1)
Output:
(1, 2, 3, 4, 5, 6, 7, 8, 9)

Remove an element from a list at a specific index

tuple1 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
tuple1.pop(0)
print(tuple1)
Output:
(2, 3, 4, 5, 6, 7, 8, 9, 10)

Remove an element from a list using del

tuple1 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
del tuple1[0]
print(tuple1)
Output:
(2, 3, 4, 5, 6, 7, 8, 9, 10)

Remove all elements from a list

tuple1 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
tuple1.clear()
print(tuple1)
Output:
()

Check if an element is in a list

tuple1 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
print(11 in tuple1)
print(1 in tuple1)
Output:
False
True

Get the length of a list

tuple1 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
print(len(tuple1))
Output:
10

Sort a list

tuple1 = (6, 7, 8, 9, 10, 1, 2, 3, 4, 5)
tuple1.sort()
print(tuple1)
Output:
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

Sort a list in reverse order

tuple1 = (6, 7, 8, 9, 10, 1, 2, 3, 4, 5)
tuple1.sort(reverse=True)
print(tuple1)
Output:
(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)

Reverse a list

tuple1 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
tuple1.reverse()
print(tuple1)
Output:
(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)

Copy a list

tuple1 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
tuple2 = tuple1.copy()
print(tuple2)
Output:
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

Join two lists

tuple1 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
tuple2 = (11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
tuple3 = tuple1 + tuple2
print(tuple3)
print(type(tuple3))
Output:
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
<class 'tuple'>

Join two lists using extend

tuple1 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
tuple2 = (11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
tuple1.extend(tuple2)
print(tuple1)
Output:
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)

Remove duplicates from a list

Set is a collection of unique elements. set is used to remove duplicates from a list.

tuple1 = (1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10)
tuple1 = tuple(set(tuple1))
print(tuple1)
Output:
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

Find the index of an element in a list

tuple1 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
print(tuple1.index(5))
Output:
4

Was this page helpful?