Python Data Types: Collections - Exercises, Practice, Solution
Python Collections [ 36 exercises with solution]
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
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']
Click me to see the sample solution
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)]
Click me to see the sample solution
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
Click me to see the sample solution
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)]
Click me to see the sample solution
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
1 1 1 1 1
Click me to see the sample solution
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
Click me to see the sample solution
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'])
Click me to see the sample solution
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 'collections.deque'>
Click me to see the sample solution
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])
Click me to see the sample solution
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
Click me to see the sample solution
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
Click me to see the sample solution
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
Click me to see the sample solution
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])
Click me to see the sample solution
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])
Click me to see the sample solution
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
Click me to see the sample solution
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
Click me to see the sample solution
17. Write a Python program to find the majority element from a given array of size n using the Collections module.
Sample Output:
10
Click me to see the sample solution
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'}
Click me to see the sample solution
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
Click me to see the sample solution
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)
Click me to see the sample solution
21. Write a Python program to count the most and least common characters in a given string.
Sample Output:
Original string:
hello world
Most common character of the said string: l
Least common character of the said string: h
Click me to see the sample solution
22. Write a Python program to insert an element at the beginning of a given Ordered Dictionary.
Sample Output:
Original OrderedDict:
OrderedDict([('color1', 'Red'), ('color2', 'Green'), ('color3', 'Blue')])
Insert an element at the beginning of the said OrderedDict:
Updated OrderedDict:
OrderedDict([('color4', 'Orange'), ('color1', 'Red'), ('color2', 'Green'), ('color3', 'Blue')])
Click me to see the sample solution
23. Write a Python program to get the frequency of the tuples in a given list.
Sample Output:
Original list of tuples:
[(['1', '4'], ['4', '1'], ['3', '4'], ['2', '7'], ['6', '8'], ['5', '8'], ['6', '8'], ['5', '7'], ['2', '7'])]
Tuples frequency
('1', '4') 2
('3', '4') 1
('2', '7') 2
('6', '8') 2
('5', '8') 1
('5', '7') 1
Click me to see the sample solution
24. Write a Python program to calculate the maximum aggregate from the list of tuples (pairs).
Sample Output:
Original list:
[('Juan Whelan', 90), ('Sabah Colley', 88), ('Peter Nichols', 7), ('Juan Whelan', 122), ('Sabah Colley', 84)]
Maximum aggregate value of the said list of tuple pair:
('Juan Whelan', 212)
Click me to see the sample solution
25. Write a Python program to find the characters in a list of strings that occur more or less than a given number.
Sample Output:
Original list:
['abcd', 'iabhef', 'dsalsdf', 'sdfsas', 'jlkdfgd']
Characters of the said list of strings which occur more than: 3
['a', 'd', 'f']
Characters of the said list of strings which occur less than: 3
['c', 'b', 'h', 'e', 'i', 's', 'l', 'k', 'j', 'g']
Click me to see the sample solution
26. Write a Python program to find the difference between two lists including duplicate elements. Use the collections module.
Sample Output:
Original lists:
[3, 3, 4, 7]
Click me to see the sample solution
27. Write a Python program to remove duplicate words from a given string. Use the collections module.
Sample Output:
Original String:
Python Exercises Practice Solution Exercises
After removing duplicate words from the said string:
Python Exercises Practice Solution
Click me to see the sample solution
28. Write a Python program to create a dictionary grouping a sequence of key-value pairs into a dictionary of lists. Use the collections module.
Sample Output:
Original list:
[('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
Grouping a sequence of key-value pairs into a dictionary of lists:
defaultdict(<class 'list'>, {'yellow': [1, 3], 'blue': [2, 4], 'red': [1]})
Click me to see the sample solution
29. Write a Python program to get the frequency of elements in a given list of lists. Use the collections module.
Sample Output:
Original list of lists:
[[1, 2, 3, 2], [4, 5, 6, 2], [7, 1, 9, 5]]
Frequency of the elements in the said list of lists:
Counter({2: 3, 1: 2, 5: 2, 3: 1, 4: 1, 6: 1, 7: 1, 9: 1})
Click me to see the sample solution
30. Write a Python program to count the occurrences of each element in a given list.
Sample Output:
Original List:
['Green', 'Red', 'Blue', 'Red', 'Orange', 'Black', 'Black', 'White', 'Orange']
Count the occurrence of each element of the said list:
Counter({'Red': 2, 'Orange': 2, 'Black': 2, 'Green': 1, 'Blue': 1, 'White': 1})
Original List:
[3, 5, 0, 3, 9, 5, 8, 0, 3, 8, 5, 8, 3, 5, 8, 1, 0, 2]
Count the occurrence of each element of the said list:
Counter({3: 4, 5: 4, 8: 4, 0: 3, 9: 1, 1: 1, 2: 1})
Click me to see the sample solution
31. Write a Python program to count the most common words in a dictionary.
Sample Output:
[('pink', 6), ('black', 5), ('white', 5), ('red', 4)]
Click me to see the sample solution
32. Write a Python program to find the class wise roll number from a tuple-of-tuples.
Sample Output:
defaultdict(<class 'list'>, {'V': [1, 2], 'VI': [1, 2, 3], 'VII': [1]})
Click me to see the sample solution
33. Write a Python program to count the number of students in an individual class.
Sample Output:
Counter({'VI': 3, 'V': 2, 'VII': 1})
Click me to see the sample solution
34. Write a Python program to create an instance of an OrderedDict using a given dictionary. Sort the dictionary during the creation and print the members of the dictionary in reverse order.
Sample Output:
Afghanistan 93
Albania 355
Algeria 213
Andorra 376
Angola 244
In reverse order:
Angola 244
Andorra 376
Algeria 213
Albania 355
Afghanistan 93
Click me to see the sample solution
35. Write a Python program to group a sequence of key-value pairs into a dictionary of lists.
Sample Output:
[('v', [1, 3]), ('vi', [2, 4]), ('vii', [1])]
Click me to see the sample solution
36. Write a Python program to compare two unordered lists (not sets).
Sample Output:
False
Click me to see the sample solution
Python Code Editor:
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
Test your Python skills with w3resource's quiz
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/collections/index.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics