Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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.]

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 iterate over elements repeating each as many times as its count. Go to the editor
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 of a specified text. Go to the editor
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. Go to the editor
Sample Output:
a
e
i
o
u
Click me to see the sample solution

4. Write a Python program to find the occurrences of 10 most common words in a given text. Go to the editor
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 accept some words and count the number of distinct words. Print the number of distinct words and number of occurrences for each distinct word according to their appearance. Go to the editor
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 number of subjects, subject names and marks. Input number of subjects and then subject name, marks separated by a space in next line. Print subject name and marks in order of its first occurrence. Go to the editor
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 few elements to the left and right, then remove some elements from the left, right sides and reverse the deque. Go to the editor
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. Go to the editor
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 number of elements to a deque object from an iterable object. Go to the editor
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. Go to the editor
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 to copy of a deque object and verify the shallow copying process. Go to the editor
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 presents in a deque object. Go to the editor
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 specified number (positive) of times. Go to the editor
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 specified number (negative) of times. Go to the editor
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 of a given list. Go to the editor
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. Go to the editor
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 Collections module. Go to the editor
Sample Output:
10
Click me to see the sample solution

18. Write a Python program to merge more than one dictionary in a single expression. Go to the editor
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. Go to the editor
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 maximum frequency in a given list. Go to the editor
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 most and least common characters in a given string. Go to the editor
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 OrderedDictionary. Go to the editor
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. Go to the editor
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). Go to the editor
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 which occur more than and less than a given number. Go to the editor
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 list including duplicate elements. Use collections module. Go to the editor
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 collections module. Go to the editor
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 collections module. Go to the editor
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 the elements in a given list of lists. Use collections module. Go to the editor
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 occurrence of each element of a given list. Go to the editor
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. Go to the editor
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. Go to the editor
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 of individual class. Go to the editor
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. Go to the editor
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. Go to the editor
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). Go to the editor
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



Python: Tips of the Day

Find current directory and file's directory:

To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)

To get the current working directory use

import os
cwd = os.getcwd()

Documentation references for the modules, constants and functions used above:

  • The os and os.path modules.
  • The __file__ constant
  • os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
  • os.path.dirname(path) (returns "the directory name of pathname path")
  • os.getcwd() (returns "a string representing the current working directory")
  • os.chdir(path) ("change the current working directory to path")

Ref: https://bit.ly/3fy0R6m