Hướng dẫn add tuple values python

In Python, since tuple is immutable, you cannot update it, i.e., you cannot add, change, or remove items (elements) in tuple.

tuple represents data that you don't need to update, so you should use list rather than tuple if you need to update it. However, if you really need to update tuple, you can convert it to list, update it, and then turn it back into tuple.

This article describes the following contents.

  • tuple is immutable
  • Append an item to tuple
  • Add/insert items to tuple
  • Change items in tuple
  • Remove items in tuple

Note that, although words such as "add", "change", and "remove" are used for convenience, in reality, a new object is generated, and the original object is not updated.

tuple is immutable

Use the following tuple as an example.

t = (0, 1, 2)

print(t)
# (0, 1, 2)

print(type(t))
# 

You can get elements by index [] or slice [:] like lists.

  • How to slice a list, string, tuple in Python

print(t[0])
# 0

print(t[:2])
# (0, 1)

Since tuple is immutable, you cannot assign a new value to an element.

# t[0] = 100
# TypeError: 'tuple' object does not support item assignment

Destructive methods (= methods that update the original object) such as append() in list are not defined in tuple.

# t.append(100)
# AttributeError: 'tuple' object has no attribute 'append'

Append an item to tuple

tuple is immutable, but you can concatenate multiple tuples with the + operator. At this time, the original object remains unchanged, and a new object is generated.

t_add = t + (3, 4, 5)

print(t_add)
# (0, 1, 2, 3, 4, 5)

print(t)
# (0, 1, 2)

Only tuples can be concatenated. It cannot be concatenated with other types such as list.

# print(t + [3, 4, 5])
# TypeError: can only concatenate tuple (not "list") to tuple

If you want to append an item to a tuple, you can concatenate a tuple with one element.

t_add_one = t + (3,)

print(t_add_one)
# (0, 1, 2, 3)

Note that a tuple with one element requires a comma at the end.

  • A tuple with one element requires a comma in Python

Add/insert items to tuple

If you want to add new items at the beginning or the end to tuple, you can concatenate it with the + operator as described above, but if you want to insert a new item at any position, you need to convert a tuple to a list.

Convert tuple to list with list().

  • Convert list and tuple to each other in Python

l = list(t)

print(l)
# [0, 1, 2]

print(type(l))
# 

Insert an item with insert().

  • Add an item to a list in Python (append, extend, insert)

l.insert(2, 100)

print(l)
# [0, 1, 100, 2]

Convert list to tuple with tuple().

t_insert = tuple(l)

print(t_insert)
# (0, 1, 100, 2)

print(type(t_insert))
# 

Change items in tuple

You can change items of the tuple in the same way.

Convert tuple to list, update it, and turn it back into tuple.

l = list(t)
l[1] = 100
t_change = tuple(l)

print(t_change)
# (0, 100, 2)

Remove items in tuple

You can also remove items of the tuple in the same way.

l = list(t)
l.remove(1)
t_remove = tuple(l)

print(t_remove)
# (0, 2)

In the above example, remove() is used, but you can also use pop() and del.

  • Remove an item from a list in Python (clear, pop, remove, del)