What are the methods used for addition or removal of a item inside list/tuple data structure? | Python Language Forum
Y
Yogesh Chawla Posted on 20/01/2024

What are the methods used for the addition or removal of a item inside list/tuple data structure?


Y
Yogesh Chawla Replied on 20/01/2024

#Update tuple in Python (Add, change or remove items or values in tuples and list)
#In Python, tuples are immutable. This means that you cannot modify them directly by adding, changing, or removing items (elements). If you need to work with mutable data, use list instead.
#However, if you must update a tuple, you can convert it to a list, perform the required modifications, and then convert it back to a tuple.
 
t = (0, 1, 2)
print(t) # (0, 1, 2)
print(type(t)) # <class 'tuple'>
 
#To access elements in a tuple, you can use indexing [] or slicing [:], just as you would with lists.
#How to slice a list, string, tuple in Python
print(t[0])# 0
print(t[:2])# (0, 1)
#Since tuples are immutable, you cannot assign a new value to an element.
 
#Destructive methods that modify the original object, such as append() in lists are not available in tuples.
 
#t.append(100)
#AttributeError: 'tuple' object has no attribute 'append'
#Append an item to a tuple
#Although tuples are immutable, you can concatenate them using the + operator.
 In this process, the original object remains unchanged, and a new object is created.
 
t = (0, 1, 2)
t_add = t + (3, 4, 5)
print(t_add)
# (0, 1, 2, 3, 4, 5)
print(t)
# (0, 1, 2)
#Tuples can only be concatenated with other tuples, not with other data types like lists.
# print(t + [3, 4, 5])
# TypeError: can only concatenate tuple (not "list") to tuple
#You can concatenate a list to a tuple by first converting the list to a tuple using tuple().
print(t + tuple([3, 4, 5]))
# (0, 1, 2, 3, 4, 5)
#tuple() can only convert iterable objects, such as lists, to tuples.
#Non-iterable data types, such as integers (int) and floating-point numbers (float), cannot
#be converted.
 
# print(t + tuple(3))
# TypeError: 'int' object is not iterable
#To append an item to a tuple, concatenate it as a single-element tuple.
print(t + (3,))
# (0, 1, 2, 3)
#Remember that a single-element tuple requires a trailing comma.
 
#A tuple with one element requires a comma in Python
#Add/insert items into a tuple
#To add new items at the beginning or end of a tuple, use the + operator as mentioned above.
#However, to insert a new item at any position, you must first convert the tuple to a list.
 
#Convert a tuple to a list using list().
#Convert between a list and a tuple in Python
t = (0, 1, 2)
l = list(t)
print(l)
# [0, 1, 2]
 
print(type(l))
# <class 'list'>
#Insert an item using insert().
 
#Add an item to a list in Python (append, extend, insert)
l.insert(2, 100)
print(l)
# [0, 1, 100, 2]
 
#Convert the list back to a tuple with tuple().
t_insert = tuple(l)
print(t_insert)
# (0, 1, 100, 2)
print(type(t_insert))
# <class 'tuple'>
 
#Change items in a tuple
#You can change items in a tuple using the same approach. Convert the tuple to a list, update it,
#and then convert it back to a tuple.
 
t = (0, 1, 2)
l = list(t)
l[1] = 100
t_change = tuple(l)
 
print(t_change)
# (0, 100, 2)
 
#Remove items from a tuple
#Similarly, you can remove items from a tuple using the same method.
 
t = (0, 1, 2)
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.