(1) What Is A Tuple Data Structure?

  • Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are ListSet, and Dictionary, all with different qualities and usage.
  • Tuples are used to store multiple items in a single variable.
  • A tuple is a collection which is ordered and unchangeable.

Example-1

tp = (1,'abc',4.5,[9,8])

print(type(tp))

print(tp)
<class 'tuple'>
(1, 'abc', 4.5, [9, 8])

(2) Find The Size Of The Tuple In Python.

  • getsizeof() Function can be used to get the size of the tuple.
  • The getsizeof() function belongs to the python’s sys module.

Example-1

import sys

Tuple1 = ("A", 1, "B", 2, "C", 3)
Tuple2 = ("Geek1", "Raju", "Geek2", "Nikhil", "Geek3", "Deepanshu")
Tuple3 = ((1, "Lion"), ( 2, "Tiger"), (3, "Fox"), (4, "Wolf"))

print("Size of Tuple1: " + str(sys.getsizeof(Tuple1)) + "bytes")
print("Size of Tuple2: " + str(sys.getsizeof(Tuple2)) + "bytes")
print("Size of Tuple3: " + str(sys.getsizeof(Tuple3)) + "bytes")
Size of Tuple1: 88bytes
Size of Tuple2: 88bytes
Size of Tuple3: 72bytes

(3) Adding Tuple to List and Vice – Versa

  • Using += operator we can join List and Tuple.
  • This operator can be used to join a list with a tuple.
  • Internally its working is similar to that of list.extend(), which can have any iterable as its argument, tuple in this case.

Example-1

test_list = [5, 6, 7]
test_tup = (9, 10)

test_list += test_tup

test_list
[5, 6, 7, 9, 10]

(4) Find The Sum Of Tuple Elements

  • We can use sum() function to find sum of tuple elements.

Example-1

test_tup = (15, 2, 8, 4, 3, 8)

sum(test_tup)
40

(5) Find Modulo Of Two Tuple Elements

  • We can use zip() method to iterate over two tuple and can find the modulo.

Example-1

test_tup1 = (15, 5, 8, 6)

test_tup2 = (5, 6, 7, 5)

modulo = tuple(tpl1 % tpl2 for tpl1, tpl2 in zip(test_tup1,test_tup2))

print(modulo)
(0, 5, 1, 1)

(6) Update Each Element In Tuple List

  • Using list comprehension we can solve this issue.

Example-1

test_list = [(1, 3, 4), (2, 4, 6), (3, 8, 1)]

update = 5

[tuple(x + update for x in tpl) for tpl in test_list]
[(6, 8, 9), (7, 9, 11), (8, 13, 6)]

(7) Multiply Adjacent Elements Of A Tuple.

  • Using tuple comprehension we can solve this issue.

Example-1

tpl = (2,4,6,1,7)

tuple(i*j for i, j in zip(tpl, tpl[1:]))
(8, 24, 6, 7)

(8) Join Tuples If Similar Initial Element Is Present.

  • Using tuple comprehension we can solve this issue.

Example-1

test_list = [(5, 6), (5, 7), (6, 8), (6, 10), (7, 13)]

res = []

for sub in test_list:
    if res and res[-1][0] == sub[0]:
        res[-1].extend(sub[1:])
    else:
        res.append([ele for ele in sub])
        
res = list(map(tuple, res))
res
[(5, 6, 7), (6, 8, 10), (7, 13)]

Note:

  • Here res[-1][0] indicates the from the last element of the list check the 0th index.

(9) All Pair Combinations Of 2 Tuples

  • Input : test_tuple1 = (7, 2), test_tuple2 = (7, 8) 
    Output : [(7, 7), (7, 8), (2, 7), (2, 8), (7, 7), (7, 2), (8, 7), (8, 2)] 

     

  • Input : test_tuple1 = (9, 2), test_tuple2 = (7, 8) 
    Output : [(9, 7), (9, 8), (2, 7), (2, 8), (7, 9), (7, 2), (8, 9), (8, 2)]

Example-1

tuple1 = (4, 5)

tuple2 = (7, 8)

res =  [(a, b) for a in tuple1 for b in tuple2]

res = res + [(a, b) for a in tuple2 for b in tuple1]

res
[(4, 7), (4, 8), (5, 7), (5, 8), (7, 4), (7, 5), (8, 4), (8, 5)]

(10) Remove All The Tuples Of Length ‘K’.

  • Input : test_list = [(4, 5), (4, ), (8, 6, 7), (1, ), (3, 4, 6, 7)], K = 2 
    Output : [(4, ), (8, 6, 7), (1, ), (3, 4, 6, 7)] 
    Explanation : (4, 5) of len = 2 is removed.

Example-1

test_list = [(4, 5), (4, ), (8, 6, 7), (1, ), (3, 4, 6, 7)]

k = 2

[tpl for tpl in test_list if len(tpl) != k]
[(4,), (8, 6, 7), (1,), (3, 4, 6, 7)]

(11) Remove Tuples From The List Having Every Element As None

  • Input : test_list = [(None, 2), (None, None), (3, 4), (12, 3), (None, )]  Output : [(None, 2), (3, 4), (12, 3)] 

Example-1

test_list = [(None, 2), (None, None), (3, 4), (12, 3), (None, )]

[tpl for tpl in test_list if any(tpl)]
[(None, 2), (3, 4), (12, 3)]

(12) Sort A List Of Tuples By Second Item

  • Input : [('for', 24), ('Geeks', 8), ('Geeks', 30)] 
    Output : [('Geeks', 8), ('for', 24), ('Geeks', 30)]

Example-1

tple = [('!!', 24), ('Love', 10), ('I', 5), ('Subhada', 20), ('You', 15)]

temp = 0

for i in range(0, len(tple)):
    for j in range(i+1, len(tple)):
        if tple[i][1] > tple[j][1]:
            temp = tple[i]
            tple[i] = tple[j]
            tple[j] = temp
print(tple)
[('I', 5), ('Love', 10), ('You', 15), ('Subhada', 20), ('!!', 24)]

(13) Sort Tuples By Total Digits

  • Input : test_list = [(3, 4, 6, 723), (1, 2), (134, 234, 34)] 
    Output : [(1, 2), (3, 4, 6, 723), (134, 234, 34)] 
    Explanation : 2 < 6 < 8, sorted by increasing total digits.

Example-1

test_list = [(3, 4, 6, 9), (1, 2), (14, 24, 34)]

temp = 0

for i in range(0, len(test_list)):
    for j in range(i+1, len(test_list)):
        if len(test_list[i]) > len(test_list[j]):
            temp = test_list[i]
            test_list[i] = test_list[j]
            test_list[j] = temp
print(test_list)
     
[(1, 2), (14, 24, 34), (3, 4, 6, 9)]

(14) Elements Frequency In Tuple

  • Input : test_tup = (4, 5, 4, 5, 6, 6, 5) 
    Output : {4: 2, 5: 3, 6: 2} 
    Explanation : Frequency of 4 is 2 and so on..

Example-1

test_tup = (4, 5, 4, 5, 6, 6, 5) 

count = {k:test_tup.count(test_tup[v]) for v,k in enumerate(test_tup) }

count
{4: 2, 5: 3, 6: 2}

(15) Select Only Desired Length Tuple.

Example-1

test_list = [(4, ), (5, 6), (2, 3, 5), (5, 6, 8, 2), (5, 9)]

i, j = 2, 3

res = [sub for sub in test_list if len(sub) >= i and len(sub) <= j]

print(res)
[(5, 6), (2, 3, 5), (5, 9)]

(16) Test If Tuple Is Distinct.

Example-1

test_tup = (1, 4, 5, 6, 1, 4)

res = True
temp = set()
for ele in test_tup:
    if ele in temp:
        res = False
        break
    temp.add(ele)
    
print("Is tuple distinct ? : " + str(res))
Is tuple distinct ? : False

Leave a Reply

Your email address will not be published. Required fields are marked *