Consider the list of tuples L yay 3 pigs 4 at 2 cheer 5 Sta
Solution
List of tuple :: The list contains tuple as element.
Given List of tuple, L = [(\'yay\',3),(\'pigs\',4),(\'at\',2),(\'cheer\',5)]
a)
L.sort() % lsorting of list
for p,q in L:
print (p)
In this part, list is sorted tand after that iterated through the list. Normally for a List structure the sorting of the elements based on it\'s values but here list of tuple. So Interepreter consider tuple as a sigle element and the comparision occurs each element from the tuple. First it consider first element of all the tuples and if first element is same for any of the tuples then it would consider the secound element of tuple. This goes upto get the sorting.
We familiared with \'for each\' loop for list data structure right, So this is also same as for each. But here, each element of list contains two values.
Output ::::::
L.sort() ---> [(\'at\', 2), (\'cheer\', 5), (\'pigs\', 4), (\'yay\', 3)]
for p,q in L:
print (p)
-----> at, cheer, pigs, yay # Getting first element of each tuple
b)
for i in range(len(L)): # iterting through the list
t = L[i]
L[i] = (t[1],t[0]) # swaping of tuple elements
This piece of code is to swap the attributes of each tuple in given list.
After this statement the list becomes, L = [(2, \'at\'), (5, \'cheer\'), (4, \'pigs\'), (3, \'yay\')]
L.sort() # Sorting of List
After this statement the list becomes, L = [(2, \'at\'), (3, \'yay\'), (4, \'pigs\'), (5, \'cheer\')]
L.sort() # Reverse order of list
After this statement the list becomes, L = [(5, \'cheer\'), (4, \'pigs\'), (3, \'yay\'), (2, \'at\')]
C)
x = L.pop() % Pop funciton will remove & get the last element
Now x = (2, \'at\') and L = [(5, \'cheer\'), (4, \'pigs\'), (3, \'yay\')]
y = L.pop(0) % Pop funciton work insted of last element to 0th element of list
Now y = (5, \'cheer\') and L = [(4, \'pigs\'), (3, \'yay\')]
L = [x,y,L[1]] % Again adding elements to new list
Now L = [(2, \'at\'), (5, \'cheer\'), (3, \'yay\')]
d ) s = \"\" % Empty string
for p,q in L:
s = \'\' + p + s % Adding p to s but s is string & p is int
This code is adding first element of each tuple. But Actually p & s are different datatypes it leads to error. We cannont concate different datatypes.
![Consider the list of tuples, L = [(\'yay\', 3), (\'pigs\', 4), (\'at\', 2), (\'cheer\', 5)]. Starting with this list L independently for parts (a) - (d), what Consider the list of tuples, L = [(\'yay\', 3), (\'pigs\', 4), (\'at\', 2), (\'cheer\', 5)]. Starting with this list L independently for parts (a) - (d), what](/WebImages/7/consider-the-list-of-tuples-l-yay-3-pigs-4-at-2-cheer-5-sta-992797-1761510694-0.webp)
![Consider the list of tuples, L = [(\'yay\', 3), (\'pigs\', 4), (\'at\', 2), (\'cheer\', 5)]. Starting with this list L independently for parts (a) - (d), what Consider the list of tuples, L = [(\'yay\', 3), (\'pigs\', 4), (\'at\', 2), (\'cheer\', 5)]. Starting with this list L independently for parts (a) - (d), what](/WebImages/7/consider-the-list-of-tuples-l-yay-3-pigs-4-at-2-cheer-5-sta-992797-1761510694-1.webp)