20. How to Use Tuples and Convert Tuple to List in Python (Video in Khmer)
Learn Python tuples with simple examples. Use tuple methods, unpack values, and convert between tuple and list.
2026-10-26 ยท 4 min read
Hello friend! ๐ A tuple keep many items together. It look like a list, but you cannot change its items.
In this post, I show you how to make and use tuples, and change them to lists. ๐
๐ฆ Create a Tuple
Use parentheses () to make a tuple:
colors = ("red", "blue", "green")
print(colors)
Output:
('red', 'blue', 'green')
๐ฅ Video: Introduction to Tuple in Python (Khmer)
๐ Get Tuple Items
Tuple index start from 0. Use a negative index to count from the end.
colors = ("red", "blue", "green")
print(colors[0]) # red
print(colors[-1]) # green
print(colors[0:2]) # ('red', 'blue')
print(len(colors)) # 3
๐งฐ Tuple Methods
Tuples have two common methods: count() and index().
numbers = (2, 5, 2, 8)
print(numbers.count(2)) # 2
print(numbers.index(5)) # 1
๐ฅ Video: Tuple Operations and Methods (Khmer)
๐ค Unpack a Tuple
Put one variable for each item to unpack a tuple:
person = ("Dara", 20)
name, age = person
print(name) # Dara
print(age) # 20
The number of variables must match the number of items.
๐ Nested Tuple
A tuple can have another tuple inside it:
students = (("Dara", 90), ("Mina", 95))
print(students[0]) # ('Dara', 90)
print(students[1][0]) # Mina
๐ฅ Video: Unpacking and Nested Tuples (Khmer)
๐ Convert Tuple and List
Use list() to change a tuple to a list. A list can change.
colors = ("red", "blue")
color_list = list(colors)
color_list.append("green")
print(color_list) # ['red', 'blue', 'green']
Use tuple() to change it back:
colors_again = tuple(color_list)
print(colors_again) # ('red', 'blue', 'green')
๐ฅ Video: Convert Tuple to List and List to Tuple (Khmer)
โ Common Error
You cannot change a tuple item:
colors = ("red", "blue")
# colors[0] = "green" # TypeError
Convert it to a list, change the list, then convert back if needed:
colors = list(colors)
colors[0] = "green"
colors = tuple(colors)
print(colors) # ('green', 'blue')
For a tuple with one item, add a comma:
one_item = ("apple",)
๐ก Tips
- Use a tuple when the items should not change.
- Use parentheses
()to make a tuple. - A tuple with one item need a comma:
("apple",). - Use square brackets
[]to get items by index. - Use
list()if you need to change tuple items. - Use
tuple()to change a list back to a tuple. - You can unpack a tuple into variables.
โ Any Question?
Tell us in comment below! ๐
#Python #Tuple #List #LearnPython #Beginner