Hướng dẫn can you modify a list in a tuple python? - bạn có thể sửa đổi danh sách trong một tuple python không?

Tuples là bất biến - bạn không thể thay đổi cấu trúc của chúng

>>> a = []
>>> tup = (a,)
>>> tup[0] is a # tup stores the reference to a
True
>>> tup[0] = a # ... but you can't re-assign it later
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'tuple' object does not support item assignment
>>> tup[0] = 'string' # ... same for all other objects
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'tuple' object does not support item assignment

hoặc kích thước

>>> del tup[0] # Nuh uh
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'tuple' object does not support item deletion
>>> id(tup)
139763805156632
>>> tup += ('something',) # works, because it creates a new tuple object:
>>> id(tup) # ... the id is different
139763805150344

Sau khi bạn tạo ra chúng.

Mặt khác, các vật thể có thể thay đổi được lưu trữ trong một tuple không mất khả năng đột biến của chúng, ví dụ: Bạn vẫn có thể sửa đổi danh sách bên trong bằng các phương thức danh sách:

>>> a = []
>>> b, c = (a,), (a,) # references to a, not copies of a
>>> b[0].append(1)
>>> b
([1],)
>>> c
([1],)

Tuples có thể lưu trữ bất kỳ loại đối tượng nào, mặc dù các bộ dữ liệu có chứa danh sách (hoặc bất kỳ đối tượng có thể thay đổi nào khác) không thể băm:

>>> hash(b)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unhashable type: 'list'

Hành vi được thể hiện ở trên thực sự có thể dẫn đến các lỗi khó hiểu.

Làm cách nào để thay đổi danh sách trong tuple?

Sử dụng hàm tích hợp tuple () có thể được truyền dưới dạng đầu vào cho hàm tuple (), sẽ chuyển đổi nó thành một đối tượng tuple. Nếu bạn muốn chuyển đổi danh sách Python thành Tuple, bạn có thể sử dụng hàm tuple () để chuyển danh sách đầy đủ dưới dạng đối số và nó sẽ trả về kiểu dữ liệu Tuple dưới dạng đầu ra.
W3Schools is Powered by W3.CSS.

Có điều đó là có thể, bởi vì khi chúng ta truy cập các yếu tố danh sách có thể thay đổi. Các bộ dữ liệu là bất biến theo mặc định và bạn không được phép thay đổi các phần tử tuple, nhưng bạn có thể sửa đổi nội dung bên trong danh sách là một yếu tố trong tuple. Tuples are immutable by default and you are not allowed by the python to change tuple elements, but you can modify the contents inside the list which is an one element in tuple.

Với một danh sách

>>> x = [1,2,3]
>>> x[0] = 5
>>> x
[5, 2, 3]

Với một tuple

>>> y = tuple([1,2,3])
>>> y
(1, 2, 3)
>>> y[0] = 5   # Not allowed!

Traceback (most recent call last):
  File "", line 1, in 
    y[0] = 5
TypeError: 'tuple' object does not support item assignment

Ví dụ 1: (Bạn được phép sửa đổi các yếu tố danh sách bên trong Tuple)

>>> a = ([1, 2, 3], [4, 5, 6])
>>>
>>> print type(a)
>>>
>>>
>>> print type(a[0])
>>>
>>>
>>> Here a[0] is the tuple 0th element, which is immutable
>>> a[0]
>>> [1, 2, 3]
>>>
>>> a[0] = 5
>>> Traceback (most recent call last):
>>> File "", line 1, in
>>> TypeError: 'tuple' object does not support item assignment
>>>
>>> Here, we accessing the list 0th element in tuple -> a[0]
>>> a[0][0] = 10
>>> a
>>> ([10, 2, 3], [4, 5, 6])
>>>
>>> a = ([1, 2, 3], [4, 5, 6], 2,4,'string')
>>>
>>> a[1][1] = 20
>>> a
>>> ([1, 2, 3], [4, 20, 6], 2, 4, 'string')

Ví dụ 2: (Bạn được phép sửa đổi các yếu tố danh sách bên trong Tuple)

>>> a = [1,2,3]
>>> b = [4,5,6]
>>> t = (a,b)
>>> t
([1, 2, 3], [4, 5, 6]) # Type is tuple

>>> t[0][0] = 5
>>> t
([5, 2, 3], [4, 5, 6])

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Phương pháp số 1: Sử dụng ________ 11+ Danh sách hiểu Phương pháp này, chúng tôi chỉ lấy phần tử đầu tiên của danh sách tuple và phần tử ở chỉ mục tương ứng và zip chúng lại với nhau bằng hàm zip.
    In this method, we just take the first element of list of tuple and the element at corresponding index and zip them together using the zip function.

    >>> del tup[0] # Nuh uh
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'tuple' object does not support item deletion
    >>> id(tup)
    139763805156632
    >>> tup += ('something',) # works, because it creates a new tuple object:
    >>> id(tup) # ... the id is different
    139763805150344
    
    2
    >>> del tup[0] # Nuh uh
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'tuple' object does not support item deletion
    >>> id(tup)
    139763805156632
    >>> tup += ('something',) # works, because it creates a new tuple object:
    >>> id(tup) # ... the id is different
    139763805150344
    
    3
    >>> del tup[0] # Nuh uh
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'tuple' object does not support item deletion
    >>> id(tup)
    139763805156632
    >>> tup += ('something',) # works, because it creates a new tuple object:
    >>> id(tup) # ... the id is different
    139763805150344
    
    4
    >>> del tup[0] # Nuh uh
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'tuple' object does not support item deletion
    >>> id(tup)
    139763805156632
    >>> tup += ('something',) # works, because it creates a new tuple object:
    >>> id(tup) # ... the id is different
    139763805150344
    
    5
    >>> del tup[0] # Nuh uh
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'tuple' object does not support item deletion
    >>> id(tup)
    139763805156632
    >>> tup += ('something',) # works, because it creates a new tuple object:
    >>> id(tup) # ... the id is different
    139763805150344
    
    6
    >>> del tup[0] # Nuh uh
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'tuple' object does not support item deletion
    >>> id(tup)
    139763805156632
    >>> tup += ('something',) # works, because it creates a new tuple object:
    >>> id(tup) # ... the id is different
    139763805150344
    
    7
    >>> del tup[0] # Nuh uh
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'tuple' object does not support item deletion
    >>> id(tup)
    139763805156632
    >>> tup += ('something',) # works, because it creates a new tuple object:
    >>> id(tup) # ... the id is different
    139763805150344
    
    8
    >>> del tup[0] # Nuh uh
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'tuple' object does not support item deletion
    >>> id(tup)
    139763805156632
    >>> tup += ('something',) # works, because it creates a new tuple object:
    >>> id(tup) # ... the id is different
    139763805150344
    
    9
    >>> del tup[0] # Nuh uh
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'tuple' object does not support item deletion
    >>> id(tup)
    139763805156632
    >>> tup += ('something',) # works, because it creates a new tuple object:
    >>> id(tup) # ... the id is different
    139763805150344
    
    6______21

    >>> a = []
    >>> b, c = (a,), (a,) # references to a, not copies of a
    >>> b[0].append(1)
    >>> b
    ([1],)
    >>> c
    ([1],)
    
    7
    >>> del tup[0] # Nuh uh
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'tuple' object does not support item deletion
    >>> id(tup)
    139763805156632
    >>> tup += ('something',) # works, because it creates a new tuple object:
    >>> id(tup) # ... the id is different
    139763805150344
    
    3
    >>> a = []
    >>> b, c = (a,), (a,) # references to a, not copies of a
    >>> b[0].append(1)
    >>> b
    ([1],)
    >>> c
    ([1],)
    
    9
    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    0
    >>> del tup[0] # Nuh uh
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'tuple' object does not support item deletion
    >>> id(tup)
    139763805156632
    >>> tup += ('something',) # works, because it creates a new tuple object:
    >>> id(tup) # ... the id is different
    139763805150344
    
    6
    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    2
    >>> del tup[0] # Nuh uh
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'tuple' object does not support item deletion
    >>> id(tup)
    139763805156632
    >>> tup += ('something',) # works, because it creates a new tuple object:
    >>> id(tup) # ... the id is different
    139763805150344
    
    6
    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    4
    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    5

    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    6
    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    7
    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    8
    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    9
    >>> x = [1,2,3]
    >>> x[0] = 5
    >>> x
    [5, 2, 3]
    
    0
    >>> x = [1,2,3]
    >>> x[0] = 5
    >>> x
    [5, 2, 3]
    
    1

    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    6
    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    7
    >>> x = [1,2,3]
    >>> x[0] = 5
    >>> x
    [5, 2, 3]
    
    4
    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    9
    >>> x = [1,2,3]
    >>> x[0] = 5
    >>> x
    [5, 2, 3]
    
    0
    >>> x = [1,2,3]
    >>> x[0] = 5
    >>> x
    [5, 2, 3]
    
    7

    >>> x = [1,2,3]
    >>> x[0] = 5
    >>> x
    [5, 2, 3]
    
    8
    >>> del tup[0] # Nuh uh
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'tuple' object does not support item deletion
    >>> id(tup)
    139763805156632
    >>> tup += ('something',) # works, because it creates a new tuple object:
    >>> id(tup) # ... the id is different
    139763805150344
    
    3
    >>> y = tuple([1,2,3])
    >>> y
    (1, 2, 3)
    >>> y[0] = 5   # Not allowed!
    
    Traceback (most recent call last):
      File "", line 1, in 
        y[0] = 5
    TypeError: 'tuple' object does not support item assignment
    
    0
    >>> y = tuple([1,2,3])
    >>> y
    (1, 2, 3)
    >>> y[0] = 5   # Not allowed!
    
    Traceback (most recent call last):
      File "", line 1, in 
        y[0] = 5
    TypeError: 'tuple' object does not support item assignment
    
    1
    >>> y = tuple([1,2,3])
    >>> y
    (1, 2, 3)
    >>> y[0] = 5   # Not allowed!
    
    Traceback (most recent call last):
      File "", line 1, in 
        y[0] = 5
    TypeError: 'tuple' object does not support item assignment
    
    2
    >>> y = tuple([1,2,3])
    >>> y
    (1, 2, 3)
    >>> y[0] = 5   # Not allowed!
    
    Traceback (most recent call last):
      File "", line 1, in 
        y[0] = 5
    TypeError: 'tuple' object does not support item assignment
    
    3

    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    6
    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    7
    >>> a = ([1, 2, 3], [4, 5, 6])
    >>>
    >>> print type(a)
    >>>
    >>>
    >>> print type(a[0])
    >>>
    >>>
    >>> Here a[0] is the tuple 0th element, which is immutable
    >>> a[0]
    >>> [1, 2, 3]
    >>>
    >>> a[0] = 5
    >>> Traceback (most recent call last):
    >>> File "", line 1, in
    >>> TypeError: 'tuple' object does not support item assignment
    >>>
    >>> Here, we accessing the list 0th element in tuple -> a[0]
    >>> a[0][0] = 10
    >>> a
    >>> ([10, 2, 3], [4, 5, 6])
    >>>
    >>> a = ([1, 2, 3], [4, 5, 6], 2,4,'string')
    >>>
    >>> a[1][1] = 20
    >>> a
    >>> ([1, 2, 3], [4, 20, 6], 2, 4, 'string')
    0
    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    9
    >>> x = [1,2,3]
    >>> x[0] = 5
    >>> x
    [5, 2, 3]
    
    0
    >>> a = ([1, 2, 3], [4, 5, 6])
    >>>
    >>> print type(a)
    >>>
    >>>
    >>> print type(a[0])
    >>>
    >>>
    >>> Here a[0] is the tuple 0th element, which is immutable
    >>> a[0]
    >>> [1, 2, 3]
    >>>
    >>> a[0] = 5
    >>> Traceback (most recent call last):
    >>> File "", line 1, in
    >>> TypeError: 'tuple' object does not support item assignment
    >>>
    >>> Here, we accessing the list 0th element in tuple -> a[0]
    >>> a[0][0] = 10
    >>> a
    >>> ([10, 2, 3], [4, 5, 6])
    >>>
    >>> a = ([1, 2, 3], [4, 5, 6], 2,4,'string')
    >>>
    >>> a[1][1] = 20
    >>> a
    >>> ([1, 2, 3], [4, 20, 6], 2, 4, 'string')
    3

    Đầu ra:

    The original list 1 : [('Geeks', 1), ('for', 2), ('Geeks', 3)]
    The original list 2 : [4, 5, 6]
    The modified resultant list of tuple : [('Geeks', 4), ('for', 5), ('Geeks', 6)]
    

    Phương pháp số 2: Sử dụng chức năng itemgetter

    >>> a = ([1, 2, 3], [4, 5, 6])
    >>>
    >>> print type(a)
    >>>
    >>>
    >>> print type(a[0])
    >>>
    >>>
    >>> Here a[0] is the tuple 0th element, which is immutable
    >>> a[0]
    >>> [1, 2, 3]
    >>>
    >>> a[0] = 5
    >>> Traceback (most recent call last):
    >>> File "", line 1, in
    >>> TypeError: 'tuple' object does not support item assignment
    >>>
    >>> Here, we accessing the list 0th element in tuple -> a[0]
    >>> a[0][0] = 10
    >>> a
    >>> ([10, 2, 3], [4, 5, 6])
    >>>
    >>> a = ([1, 2, 3], [4, 5, 6], 2,4,'string')
    >>>
    >>> a[1][1] = 20
    >>> a
    >>> ([1, 2, 3], [4, 20, 6], 2, 4, 'string')
    4THE ở đây thực hiện nhiệm vụ tìm nạp hằng số của hai phần tử tuple sau đó được ánh xạ với chỉ mục tương ứng bằng cách sử dụng hàm MAP. Hàm ZIP được sử dụng để mở rộng logic này đến toàn bộ danh sách.
    The itemgetter function here does the task of fetching the constant of two tuple elements which is then mapped with the corresponding index using the map function. The zip function is used to extend this logic to the entire list.

    >>> a = ([1, 2, 3], [4, 5, 6])
    >>>
    >>> print type(a)
    >>>
    >>>
    >>> print type(a[0])
    >>>
    >>>
    >>> Here a[0] is the tuple 0th element, which is immutable
    >>> a[0]
    >>> [1, 2, 3]
    >>>
    >>> a[0] = 5
    >>> Traceback (most recent call last):
    >>> File "", line 1, in
    >>> TypeError: 'tuple' object does not support item assignment
    >>>
    >>> Here, we accessing the list 0th element in tuple -> a[0]
    >>> a[0][0] = 10
    >>> a
    >>> ([10, 2, 3], [4, 5, 6])
    >>>
    >>> a = ([1, 2, 3], [4, 5, 6], 2,4,'string')
    >>>
    >>> a[1][1] = 20
    >>> a
    >>> ([1, 2, 3], [4, 20, 6], 2, 4, 'string')
    5
    >>> a = ([1, 2, 3], [4, 5, 6])
    >>>
    >>> print type(a)
    >>>
    >>>
    >>> print type(a[0])
    >>>
    >>>
    >>> Here a[0] is the tuple 0th element, which is immutable
    >>> a[0]
    >>> [1, 2, 3]
    >>>
    >>> a[0] = 5
    >>> Traceback (most recent call last):
    >>> File "", line 1, in
    >>> TypeError: 'tuple' object does not support item assignment
    >>>
    >>> Here, we accessing the list 0th element in tuple -> a[0]
    >>> a[0][0] = 10
    >>> a
    >>> ([10, 2, 3], [4, 5, 6])
    >>>
    >>> a = ([1, 2, 3], [4, 5, 6], 2,4,'string')
    >>>
    >>> a[1][1] = 20
    >>> a
    >>> ([1, 2, 3], [4, 20, 6], 2, 4, 'string')
    6

    >>> del tup[0] # Nuh uh
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'tuple' object does not support item deletion
    >>> id(tup)
    139763805156632
    >>> tup += ('something',) # works, because it creates a new tuple object:
    >>> id(tup) # ... the id is different
    139763805150344
    
    2
    >>> del tup[0] # Nuh uh
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'tuple' object does not support item deletion
    >>> id(tup)
    139763805156632
    >>> tup += ('something',) # works, because it creates a new tuple object:
    >>> id(tup) # ... the id is different
    139763805150344
    
    3
    >>> del tup[0] # Nuh uh
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'tuple' object does not support item deletion
    >>> id(tup)
    139763805156632
    >>> tup += ('something',) # works, because it creates a new tuple object:
    >>> id(tup) # ... the id is different
    139763805150344
    
    4
    >>> del tup[0] # Nuh uh
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'tuple' object does not support item deletion
    >>> id(tup)
    139763805156632
    >>> tup += ('something',) # works, because it creates a new tuple object:
    >>> id(tup) # ... the id is different
    139763805150344
    
    5
    >>> del tup[0] # Nuh uh
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'tuple' object does not support item deletion
    >>> id(tup)
    139763805156632
    >>> tup += ('something',) # works, because it creates a new tuple object:
    >>> id(tup) # ... the id is different
    139763805150344
    
    6
    >>> del tup[0] # Nuh uh
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'tuple' object does not support item deletion
    >>> id(tup)
    139763805156632
    >>> tup += ('something',) # works, because it creates a new tuple object:
    >>> id(tup) # ... the id is different
    139763805150344
    
    7
    >>> del tup[0] # Nuh uh
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'tuple' object does not support item deletion
    >>> id(tup)
    139763805156632
    >>> tup += ('something',) # works, because it creates a new tuple object:
    >>> id(tup) # ... the id is different
    139763805150344
    
    8
    >>> del tup[0] # Nuh uh
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'tuple' object does not support item deletion
    >>> id(tup)
    139763805156632
    >>> tup += ('something',) # works, because it creates a new tuple object:
    >>> id(tup) # ... the id is different
    139763805150344
    
    9
    >>> del tup[0] # Nuh uh
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'tuple' object does not support item deletion
    >>> id(tup)
    139763805156632
    >>> tup += ('something',) # works, because it creates a new tuple object:
    >>> id(tup) # ... the id is different
    139763805150344
    
    6______21

    >>> a = []
    >>> b, c = (a,), (a,) # references to a, not copies of a
    >>> b[0].append(1)
    >>> b
    ([1],)
    >>> c
    ([1],)
    
    7
    >>> del tup[0] # Nuh uh
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'tuple' object does not support item deletion
    >>> id(tup)
    139763805156632
    >>> tup += ('something',) # works, because it creates a new tuple object:
    >>> id(tup) # ... the id is different
    139763805150344
    
    3
    >>> a = []
    >>> b, c = (a,), (a,) # references to a, not copies of a
    >>> b[0].append(1)
    >>> b
    ([1],)
    >>> c
    ([1],)
    
    9
    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    0
    >>> del tup[0] # Nuh uh
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'tuple' object does not support item deletion
    >>> id(tup)
    139763805156632
    >>> tup += ('something',) # works, because it creates a new tuple object:
    >>> id(tup) # ... the id is different
    139763805150344
    
    6
    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    2
    >>> del tup[0] # Nuh uh
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'tuple' object does not support item deletion
    >>> id(tup)
    139763805156632
    >>> tup += ('something',) # works, because it creates a new tuple object:
    >>> id(tup) # ... the id is different
    139763805150344
    
    6
    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    4
    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    5

    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    6
    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    7
    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    8
    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    9
    >>> x = [1,2,3]
    >>> x[0] = 5
    >>> x
    [5, 2, 3]
    
    0
    >>> x = [1,2,3]
    >>> x[0] = 5
    >>> x
    [5, 2, 3]
    
    1

    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    6
    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    7
    >>> x = [1,2,3]
    >>> x[0] = 5
    >>> x
    [5, 2, 3]
    
    4
    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    9
    >>> x = [1,2,3]
    >>> x[0] = 5
    >>> x
    [5, 2, 3]
    
    0
    >>> x = [1,2,3]
    >>> x[0] = 5
    >>> x
    [5, 2, 3]
    
    7

    >>> x = [1,2,3]
    >>> x[0] = 5
    >>> x
    [5, 2, 3]
    
    8
    >>> del tup[0] # Nuh uh
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'tuple' object does not support item deletion
    >>> id(tup)
    139763805156632
    >>> tup += ('something',) # works, because it creates a new tuple object:
    >>> id(tup) # ... the id is different
    139763805150344
    
    3
    >>> y = tuple([1,2,3])
    >>> y
    (1, 2, 3)
    >>> y[0] = 5   # Not allowed!
    
    Traceback (most recent call last):
      File "", line 1, in 
        y[0] = 5
    TypeError: 'tuple' object does not support item assignment
    
    0
    >>> y = tuple([1,2,3])
    >>> y
    (1, 2, 3)
    >>> y[0] = 5   # Not allowed!
    
    Traceback (most recent call last):
      File "", line 1, in 
        y[0] = 5
    TypeError: 'tuple' object does not support item assignment
    
    1
    >>> y = tuple([1,2,3])
    >>> y
    (1, 2, 3)
    >>> y[0] = 5   # Not allowed!
    
    Traceback (most recent call last):
      File "", line 1, in 
        y[0] = 5
    TypeError: 'tuple' object does not support item assignment
    
    2
    >>> y = tuple([1,2,3])
    >>> y
    (1, 2, 3)
    >>> y[0] = 5   # Not allowed!
    
    Traceback (most recent call last):
      File "", line 1, in 
        y[0] = 5
    TypeError: 'tuple' object does not support item assignment
    
    3

    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    6
    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    7
    >>> a = ([1, 2, 3], [4, 5, 6])
    >>>
    >>> print type(a)
    >>>
    >>>
    >>> print type(a[0])
    >>>
    >>>
    >>> Here a[0] is the tuple 0th element, which is immutable
    >>> a[0]
    >>> [1, 2, 3]
    >>>
    >>> a[0] = 5
    >>> Traceback (most recent call last):
    >>> File "", line 1, in
    >>> TypeError: 'tuple' object does not support item assignment
    >>>
    >>> Here, we accessing the list 0th element in tuple -> a[0]
    >>> a[0][0] = 10
    >>> a
    >>> ([10, 2, 3], [4, 5, 6])
    >>>
    >>> a = ([1, 2, 3], [4, 5, 6], 2,4,'string')
    >>>
    >>> a[1][1] = 20
    >>> a
    >>> ([1, 2, 3], [4, 20, 6], 2, 4, 'string')
    0
    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    9
    >>> x = [1,2,3]
    >>> x[0] = 5
    >>> x
    [5, 2, 3]
    
    0
    >>> a = ([1, 2, 3], [4, 5, 6])
    >>>
    >>> print type(a)
    >>>
    >>>
    >>> print type(a[0])
    >>>
    >>>
    >>> Here a[0] is the tuple 0th element, which is immutable
    >>> a[0]
    >>> [1, 2, 3]
    >>>
    >>> a[0] = 5
    >>> Traceback (most recent call last):
    >>> File "", line 1, in
    >>> TypeError: 'tuple' object does not support item assignment
    >>>
    >>> Here, we accessing the list 0th element in tuple -> a[0]
    >>> a[0][0] = 10
    >>> a
    >>> ([10, 2, 3], [4, 5, 6])
    >>>
    >>> a = ([1, 2, 3], [4, 5, 6], 2,4,'string')
    >>>
    >>> a[1][1] = 20
    >>> a
    >>> ([1, 2, 3], [4, 20, 6], 2, 4, 'string')
    3

    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    6
    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    7
    >>> a = ([1, 2, 3], [4, 5, 6])
    >>>
    >>> print type(a)
    >>>
    >>>
    >>> print type(a[0])
    >>>
    >>>
    >>> Here a[0] is the tuple 0th element, which is immutable
    >>> a[0]
    >>> [1, 2, 3]
    >>>
    >>> a[0] = 5
    >>> Traceback (most recent call last):
    >>> File "", line 1, in
    >>> TypeError: 'tuple' object does not support item assignment
    >>>
    >>> Here, we accessing the list 0th element in tuple -> a[0]
    >>> a[0][0] = 10
    >>> a
    >>> ([10, 2, 3], [4, 5, 6])
    >>>
    >>> a = ([1, 2, 3], [4, 5, 6], 2,4,'string')
    >>>
    >>> a[1][1] = 20
    >>> a
    >>> ([1, 2, 3], [4, 20, 6], 2, 4, 'string')
    0
    >>> hash(b)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unhashable type: 'list'
    
    9
    >>> x = [1,2,3]
    >>> x[0] = 5
    >>> x
    [5, 2, 3]
    
    0
    >>> a = ([1, 2, 3], [4, 5, 6])
    >>>
    >>> print type(a)
    >>>
    >>>
    >>> print type(a[0])
    >>>
    >>>
    >>> Here a[0] is the tuple 0th element, which is immutable
    >>> a[0]
    >>> [1, 2, 3]
    >>>
    >>> a[0] = 5
    >>> Traceback (most recent call last):
    >>> File "", line 1, in
    >>> TypeError: 'tuple' object does not support item assignment
    >>>
    >>> Here, we accessing the list 0th element in tuple -> a[0]
    >>> a[0][0] = 10
    >>> a
    >>> ([10, 2, 3], [4, 5, 6])
    >>>
    >>> a = ([1, 2, 3], [4, 5, 6], 2,4,'string')
    >>>
    >>> a[1][1] = 20
    >>> a
    >>> ([1, 2, 3], [4, 20, 6], 2, 4, 'string')
    3

    Đầu ra:

    The original list 1 : [('Geeks', 1), ('for', 2), ('Geeks', 3)]
    The original list 2 : [4, 5, 6]
    The modified resultant list of tuple : [('Geeks', 4), ('for', 5), ('Geeks', 6)]
    


    Chúng ta có thể chỉnh sửa một danh sách bên trong một tuple trong Python không?

    Khi một tuple được tạo, bạn không thể thay đổi giá trị của nó.Bộ dữ liệu không thể thay đổi, hoặc bất biến vì nó cũng được gọi.Nhưng có một cách giải quyết.Bạn có thể chuyển đổi tuple thành một danh sách, thay đổi danh sách và chuyển đổi danh sách trở lại thành một bộ.You can convert the tuple into a list, change the list, and convert the list back into a tuple.

    Làm cách nào để thay đổi danh sách trong tuple?

    Sử dụng hàm tích hợp tuple () có thể được truyền dưới dạng đầu vào cho hàm tuple (), sẽ chuyển đổi nó thành một đối tượng tuple.Nếu bạn muốn chuyển đổi danh sách Python thành Tuple, bạn có thể sử dụng hàm tuple () để chuyển danh sách đầy đủ dưới dạng đối số và nó sẽ trả về kiểu dữ liệu Tuple dưới dạng đầu ra.use the tuple() function to pass the full list as an argument, and it will return the tuple data type as an output.

    Bạn có thể đặt một danh sách bên trong một tuple?

    Chức năng Tuple () có thể lấy bất kỳ sự khác biệt nào làm đối số và chuyển đổi nó thành một đối tượng Tuple.Khi bạn muốn chuyển đổi danh sách Python thành một tuple, bạn có thể chuyển toàn bộ danh sách dưới dạng tham số trong hàm tuple () và nó sẽ trả về kiểu dữ liệu tuple dưới dạng đầu ra.you can pass the entire list as a parameter within the tuple() function, and it will return the tuple data type as an output.