Add Element to Tuple in Python
Python:
Popular high-level, all-purpose programming language Python. The new version of the Python programming language, Python 3, is used for all software applications, including web development. Python is the best programming language for newcomers and seasoned C++ and Java programmers. Python is a well-known high-level, general-purpose language used for various tasks, including web development, scraping, and GUI creation. Although many Python tutorials are many Python tutorials that go into great detail, it might not be enough for you to understand this language fully.
Tuple in Python:
Objects are divided up into groups called Python Tuples by commas. In terms of repetition, nested objects, and indexing, a tuple and a list resemble each other in some ways, but just a tuple is immutable while a list is mutable. Multiple items can be stored in one variable by using tuples. One of the four built-in types of data in Python for storing data collections is the tuple; the other three are list, set, as well as dictionary, each with a unique set of features and applications. A tuple is an unchangeable, ordered collection. One of the primary container data structures offered by Python is the tuple. Typically, they are made using standard parentheses (). They are heterogeneous due to their being container data types, which can hold a variety of items and permit items of various data types.
What is Immutable mean?
Immutable is a property that cannot be changed, according to the Python documentation. If we want to append to a tuple using Python, this obviously presents a problem. In reality, changing (or mutating) an object is what we do when we wish to append a value to it. As a result, throughout this tutorial, whenever we talk about adding to a tuple in Python, we really mean adding a value to an already-existing tuple by making a new object.
CODE:
#creating a tuple s
s= (4,5,6)
print(s)
print(type(s))
OUTPUT:
(4, 5, 6)
<class 'tuple'>
Accessing Values in Python Tuples:
Accessing tuple values using index:
CODE:
tup = ("hey", "How", "are You!")
print("Value in tup[0] = ", tup[0])
print("Value in tup[1] = ", tup[1])
print("Value in tup[2] = ", tup[2])
OUTPUT:
Value in tup[0] = hey
Value in tup[1] = How
Value in tup[2] = are You!
To access the need value from tuple in Python, we used the positive index in the methods above. Here, we will use the negative index within Python.
CODE:
tp = ("Where ", "are", "you!")
print("Value in tp[-3] = ", tp[-3])
print("Value in tp[-2] = ", tp[-2])
print("Value in tp[-1] = ", tp[-1])
OUTPUT:
Value in tp[-3] = Where
Value in tp[-2] = are
Value in tp[-1] = you!
Appending elements to tuple:
Tuple is immutable, but you can combine multiple tuples by using the + operator. At this point, a new object is created, however the old object is still there.
Python tuples don't have a specific method for appending values, but we can simulate appending by concatenating multiple tuples. Despite applying it to the identical variable as before, we in fact generate a new tuple when we do this.
CODE:
s=(23,35,3)
s_append = s + (18, 36, 17)
print(s_append)
print(s)
# *******************************
# Adding a tuple to another using concatenation
tuple = (4,3,2)
tuple = tuple + (1,)
print(tuple)
OUTPUT:
(23, 35, 3, 18, 36, 17)
(23, 35, 3)
(4, 3, 2, 1)
Only tuples allow for concatenation. It cannot be combined with other types, such as lists.
EXAMPLE:
t=(2,5,8)
t_append = (t + [8, 16, 67])
print(t_append)
print(t)
OUTPUT:
Traceback (most recent call last):
File "D:\My Portfolio\test.py", line 2, in <module>
t_append = (t + [8, 16, 67])
TypeError: can only concatenate tuple (not "list") to tuple
Here, we can see that a value was added to our tuple. We achieved this by first transforming our value into a tuple. As we can just concatenate tuples to tuples, this had to be done. If we just attempted to simply add the value, a TypeError would be generated. It is important to note that this is a brand-new object, though. Using the id() function, we can use the object's id to print out its before- and after-append states to confirm this. Looking at this now
EXAMPLE:
tuple = (5, 3, 2)
print(id(tuple))
tuple = tuple + (4,)
print(id(tuple))
OUTPUT:
2065288376960
2065288098608
A tuple can be concatenated by adding new items to the start or end, as previously mentioned; however, if you want to insert a new item anywhere, you should convert a tuple to a list.
CODE:
s= (1,2,5)
# Python conversion between lists and tuples
x = list(s)
print(x)
print(type(x))
# Adding two items by using built-in function insert ()
x.insert(4, 50)
print(x)
OUTPUT:
[1, 2, 5]
<class 'list'>
[1, 2, 5, 50]