Collections module implements specialized container datatypes providing alternatives
to Python's general purpose built-in containers, dict, list, set, and tuple.
1. Write a Python program that iterates over elements as many times as its count.
Sample Output: ['p', 'p', 'p', 'p', 'q', 'q']
2. Write a Python program to find the most common elements and their counts in a specified text.
Original string: lkseropewdssafsdfafkpwe
Most common three characters of the said string:
[('s', 4), ('e', 3), ('f', 3)]
3. Write a Python program to create a new deque with three items and iterate over the deque's
elements.
Sample Output:
a
e
i
o
u
4. Write a Python program to find the occurrences of the 10 most common words in a given text.
Sample Output:
[('Python', 6), ('the', 6), ('and', 5), ('We', 2), ('with', 2), ('The', 1), ('Software', 1), ('Foundation', 1),
('PSF', 1), ('is', 1)]
5. Write a Python program that accepts some words and counts the number of distinct words. Print
the number of distinct words and the number of occurrences of each distinct word according to their
appearance.
Input number of words: 5
Input the words:
Red
Green
Blue
Black
White
5
11111
Object 1
6. Write a Python program that accepts the number of subjects, subject names and marks. Input the
number of subjects and then the subject name and marks separated by a space on the next line. Print
the subject name and marks in order of appearance.
Sample Output:
Powered by
Number of subjects: 3
Input Subject name and marks: Bengali 58
Input Subject name and marks: English 62
Input Subject name and marks: Math 68
Bengali 58
English 62
Math 68
7. Write a Python program to create a deque and append a few elements to the left and right. Next,
remove some elements from the left and right sides and reverse the deque.
Sample Output:
deque(['Red', 'Green', 'White'])
Adding to the left:
deque(['Pink', 'Red', 'Green', 'White'])
Adding to the right:
deque(['Pink', 'Red', 'Green', 'White', 'Orange'])
Removing from the right:
deque(['Pink', 'Red', 'Green', 'White'])
Removing from the left:
deque(['Red', 'Green', 'White'])
Reversing the deque:
deque(['White', 'Green', 'Red'])
8. Write a Python program to create a deque from an existing iterable object.
Sample Output:
Original tuple:
(2, 4, 6)
<class 'tuple'>
Original deque:
deque([2, 4, 6])
New deque from an existing iterable object:
deque([2, 2, 4, 6, 8, 10, 12])
<class '[Link]'>
Object 2
9. Write a Python program to add more elements to a deque object from an iterable object.
Sample Output:
Even numbers:
deque([2, 4, 6, 8, 10])
More even numbers:
deque([2, 4, 6, 8, 10, 12, 14, 16, 18, 20])
10. Write a Python program to remove all the elements of a given deque object.
Sample Output:
Original Deque object with odd numbers:
deque([1, 3, 5, 7, 9])
Deque length: 5
Deque object after removing all numbers- deque([])
Deque length:0
11. Write a Python program that copies a deque object and verifies shallow copying.
Sample Output:
Content of dq1:
deque([1, 3, 5, 7, 9])
dq2 id:
140706429557152
Content of dq2:
deque([1, 3, 5, 7, 9])
dq2 id:
140706406914152
Checking the first element of dq1 and dq2 are shallow copies:
11065888
11065888
12. Write a Python program to count the number of times a specific element appears in a deque
object.
Sample Output:
Number of 2 in the sequence
5
Number of 4 in the sequence
4
13. Write a Python program to rotate a Deque Object a specified number (positive) of times.
Sample Output:
Deque before rotation:
deque([2, 4, 6, 8, 10])
Deque after 1 positive rotation:
deque([10, 2, 4, 6, 8])
Deque after 2 positive rotations:
deque([6, 8, 10, 2, 4])
14. Write a Python program to rotate a deque Object a specified number (negative) of times.
Sample Output:
Deque before rotation:
deque([2, 4, 6, 8, 10])
Deque after 1 negative rotation:
deque([4, 6, 8, 10, 2])
Deque after 2 negative rotations:
deque([8, 10, 2, 4, 6])
15. Write a Python program to find the most common element in a given list.
Sample Output:
Original list:
['PHP', 'PHP', 'Python', 'PHP', 'Python', 'JS', 'Python', 'Python', 'PHP', 'Python']
Most common element of the said list:
Python
16. Write a Python program to find the second lowest total marks of any student(s) from the given
names and marks of each student using lists and lambda. Input number of students, names and
grades of each student.
Sample Output:
Input number of students: 3
Name: Avik Das
Total marks: 89
Name: ayan Roy
Total marks: 75
Name: Sayan Dutta
Total marks: 93
Names and Marks of all students:
[['Avik Das ', 89.0], ['ayan Roy', 75.0], ['Sayan Dutta', 93.0]]
Second lowest Marks: 89.0
Names:
Avik Das
17. Write a Python program to find the majority element from a given array of size n using the
Collections module.
Sample Output:
10
18. Write a Python program to merge more than one dictionary into a single expression.
Sample Output:
Original dictionaries:
{'R': 'Red', 'B': 'Black', 'P': 'Pink'} {'G': 'Green', 'W': 'White'}
Merged dictionary:
{'B': 'Black', 'R': 'Red', 'P': 'Pink', 'G': 'Green', 'W': 'White'}
Original dictionaries:
{'R': 'Red', 'B': 'Black', 'P': 'Pink'} {'G': 'Green', 'W': 'White'} {'O': 'Orange', 'W': 'White', 'B': 'Black'}
Merged dictionary:
{'B': 'Black', 'R': 'Red', 'P': 'Pink', 'G': 'Green', 'W': 'White', 'O': 'Orange'}
19. Write a Python program to break a given list of integers into sets of a given positive number.
Return true or false.
Sample Output:
Original list: [1, 2, 3, 4, 5, 6, 7, 8]
Number to devide the said list: 4
True
Original list: [1, 2, 3, 4, 5, 6, 7, 8]
Number to devide the said list: 3
False
20. Write a Python program to find the item with the highest frequency in a given list.
Sample Output:
Original list:
[2, 3, 8, 4, 7, 9, 8, 2, 6, 5, 1, 6, 1, 2, 3, 2, 4, 6, 9, 1, 2]
Item with maximum frequency of the said list:
(2, 5)