Tuple Functions in Python

 A tuple functions in python is one of four erected-in data types in Python used to store collections of data. Tuple operations are those that can be performed on the rudiments in the tuple data structureLet us look at some of the most extensively used tuple operations in Python.



1. Count Circumstances of an Element in a Tuple
count () system is used to count the total circumstances of an element in thetuple.However, also the function returns 0, If the element isn't plant in the tuple.

tup1 = (1, 4, 7, 3, 6, 4, 1, 8, 4)
.# counts number of times 4 occurs in the tuple
print (tup1.count (4))
# prints
3
2. Chancing the Position of an Element in a Tuple
You can use the indicator () system to find the indicatorposition of an element in thetuple.However, also the function returns the indicator of the first circumstance, If there are further than one circumstances of an element in the tuple.

tup1 = (1, 4, 7, 3, 6, 4, 1, 8, 4)
print (tup1.index (4))
# prints
1
Note

Stillalso the function throws a ValueError as
, If you try to find the indicator of the element which isn't present in the tuple.
ValueErrortuple.index (x) x not in tuple

3. How to Join Two or Further Tuples
You can join two or further tuples using the driver.

tup1 = (1, 2, 3)
. tup2 = (4, 5, 6)
. tup3 = tup1 tup2
print (tup3)
# prints
(1, 2, 3, 4, 5, 6)
.4. How to Convert String to a Tuple
You can use the tuple () constructor to convert a string to a tuple by passing the string as a parameter to the tuple () constructor.

tup1 = tuple ("MAKEUSEOF")
print (tup1)
# prints
('M','A','K','E','U','S','E','O','F')
.5. How to Convert List to a Tuple
We can follow three approaches to convert a list to a tuple.

Approach 1 Using tuple () Constructor
tuple () constructor is used to convert a list to a tuple by passing the list as a parameter to the tuple () constructor.

list1 = (1, 2, 3, 4, 5, 6)
. tup1 = tuple (list1)
print (tup1)
# prints
(1, 2, 3, 4, 5, 6)
. Approach 2 Using a Loop Inside tuple () Constructor
It's a slight variation of the below approach. We're running a circle ( using list appreciation) inside the tuple () constructor to produce the tuple.

list1 = (1, 2, 3, 4, 5, 6)
. tup1 = tuple ( element for element in list1)
print (tup1)
# prints
(1, 2, 3, 4, 5, 6)
. Approach 3 Using (* listName,)
. This unpacks a list inside the tuple nonfictional due to the presence of the single comma (,). This system is the fastest of the three approaches.

list1 = (1, 2, 3, 4, 5, 6)
. tup1 = (* list1,)
print (tup1)
# prints
(1, 2, 3, 4, 5, 6)
.6. How to Multiply Tuples
You can multiply the contents of the tuple any number of times using the * driver.

tup1 = (1, 2, 3)
. tup2 = tup1 * 3
print (tup2)
# prints
(1, 2, 3, 1, 2, 3, 1, 2, 3)
.7. How to Find Total Number of Rudiments in a Tuple
len () function is one of the most habituated inbuilt functions in Python. It's used to find the total number of particulars in an object. You can use the len () function with tuple to count the total number of rudiments in the tuple.

tup1 = (1, 2, 3)
print (len (tup1))
# prints
3
8. How to Find Minimal Element in a Tuple
min () function is used to find an element with the smallest value in the given tuple.

tup1 = (1, 2, 3)
print (" Minimal element in the tuple is")
print (min (tup1))
# prints
Minimal element in the tuple is
1
9. How to Find Maximum Element in a Tuple
maximum () function is used to find an element with the loftiest value in the given tuple.

tup1 = (1, 2, 3)
print (" Maximum element in the tuple is")
print ( maximum (tup1))
# prints
Maximum element in the tuple is
3
10. How to Find the Sum of All Rudiments in a Tuple
sum () function is used to calculate the computation sum of all rudiments in the tuple.

tup1 = (1, 2, 3)
print ("Sum of rudiments")
print ( sum (tup1))
# prints
Sum of rudiments
6
11. any () Operation on Tuples
Stillalso any () function returns True else it returns False, If one or further rudiments of the tuple have a boolean value True.

tup1 = (False, False, False, True)
print (any (tup1))
# prints
True
12. all () Operation on Tuples
You can use all () function to check if all the rudiments of the tuple have a Boolean value TrueIndeed if any one element of the tuple has a Boolean value False, also the function returns False.

tup1 = ( TrueTrueTrue, False, True)
print (all (tup1))
# prints
False
13. sorted () Operation on Tuples
You can use the sorted () function to return a sorted list in thrusting order.

tup1 = (6, 1, 8, 3, 7, 2)
print ( sorted (tup1))
print ( type ( sorted (tup1)))

prints
(1, 2, 3, 6, 7, 8)
.
Note that the return type is list
14. How to Shuffle a Tuple
Since tuples are inflexible, they can not be scuffled directly. We need to use lists to shuffle a tuple. We can shuffle a tuple using typecasting in three way

Step 1 Typecast tuple to a list

Step 2 Shuffle the list

Step 3 Typecast list back to a tuple

import arbitrary
old_tuple = (45, 46, 47, 48, 49)
.# Printing tuple
print ("Old tuple")
print (old_tuple)
# Typecasting tuple to list
list1 = list (old_tuple)
# Shuffling list
random.shuffle (list1)
# Typecasting list back to tuple
new_tuple = tuple (list1)
# Printing new scuffled tuple
print ("New scuffled tuple")
print (new_tuple)
# prints
Old tuple
(45, 46, 47, 48, 49)
. New scuffled tuple
(45, 49, 46, 47, 48)
Note Since the tuple is scuffled aimlessly, you may get a different affair.

15. How to Convert List of Tuples to List of Lists
Using the list appreciation we can convert a list of tuples to a list of lists.

list1 = ( ('A','B'), ('C','D'), ('E','F'))
print ("List of tuples")
print (list1)
# List Appreciation
result = (list ( element) for element in list1)
print ("List of lists")
print ( result)
# prints

List of tuples
( ('A','B'), ('C','D'), ('E','F'))
. List of lists
( ('A','B'), ('C','D'), ('E','F'))
.16. How to Convert List of Tuples to List of Strings
Using the list appreciation and join () system we can convert a list of tuples to a list of strings.

list1 = ( ('M','A','K','E'), ('U','S','E'), ('O','F'))
print ("List of tuples")
print (list1)
# List Appreciation with join () system
result = (''. join ( element) for element in list1)
print ("List of strings")
print ( result)
# prints

List of tuples
( ('M','A','K','E'), ('U','S','E'), ('O','F'))
. List of strings
(' MAKE',' USE','OF')
RELATED
How To Use For Circles In Python
17. How to Reverse a Tuple
Using the slicing fashion, we can reverse the tuple. A new dupe of the tuple is created during this process.

old_tuple = ('M','A','K','E','U','S','E','O','F')
print ("Old tuple")
print (old_tuple)
Reversing tuple using slicing
new_tuple = old_tuple (-1)
print ("New tuple")
print (new_tuple)
# prints
Old tuple
('M','A','K','E','U','S','E','O','F')
New tuple
('F','O','E','S','U','E','K','A','M')

Insideaiml is the platform where you can learn deeply about tuple functions and more.

Comments

Popular posts from this blog

Tanh Activation Function

Sigmoid Activation Function And Its Uses.

Unleashing Creativity: The Latest Frontier of Animation AI