This data structure exercise is for beginners to understand and practice basic data structure in Python. Practice Python List, Set, Dictionary, and Tuple questions.
The data structure is widely used to hold any data. To perform any programming tasks in Python, good knowledge of data structure is a must.
Solve this exercise to have a good understanding of basic data structure in Python
This Exercise includes the followings
- The exercise contains 10 questions and solution provided for each question
- Questions cover list manipulation, dictionary, set, and tuple methods.
Use Online Code Editor to solve exercise questions.
Exercise 1: Create a list by picking an odd-index items from the first list and even index items from the second
Given two lists, l1 and l2, write a program to create a third list l3 by picking an odd-index element from the list l1 and even index elements from the list l2.
Given:
l1 = [3, 6, 9, 12, 15, 18, 21]
l2 = [4, 8, 12, 16, 20, 24, 28]
Code language: Python (python)
Expected Output:
Element at odd-index positions from list one [6, 12, 18] Element at even-index positions from list two [4, 12, 20, 28] Printing Final third list [6, 12, 18, 4, 12, 20, 28]
Show Hint
Use list slicing
Show Solution
To access a range of items in a list, use the slicing operator :
. With this operator, you can specify where to start the slicing, end, and specify the step.
For example, the expression list1[ start : stop : step]
returns the portion of the list from index start to index stop, at a step size step.
- for 1st list: Start from the 1st index with step value 2 so it will pick elements present at index 1, 3, 5, and so on
- for 2nd list: Start from the 0th index with step value 2 so it will pick elements present at index 0, 2, 4, and so on
Exercise 2: Remove and add item in a list
Write a program to remove the item present at index 4 and add it to the 2nd position and at the end of the list.
Given:
list1 = [54, 44, 27, 79, 91, 41]
Code language: Python (python)
Expected Output:
List After removing element at index 4 [34, 54, 67, 89, 43, 94] List after Adding element at index 2 [34, 54, 11, 67, 89, 43, 94] List after Adding element at last [34, 54, 11, 67, 89, 43, 94, 11]
Show Hint
Use the list methods, pop()
, insert()
and append()
Show Solution
pop(index)
: Removes and returns the item at the given index from the list.insert(index, item)
: Add the item at the specified position(index) in the listappend(item)
: Add item at the end of the list.
Exercise 3: Slice list into 3 equal chunks and reverse each chunk
Given:
sample_list = [11, 45, 8, 23, 14, 12, 78, 45, 89]
Code language: Python (python)
Expected Outcome:
Chunk 1 [11, 45, 8] After reversing it [8, 45, 11] Chunk 2 [23, 14, 12] After reversing it [12, 14, 23] Chunk 3 [78, 45, 89] After reversing it [89, 45, 78]
Show Hint
- Divide the length of a list by 3 to get the each chunk size
- Run loop three times and use the
slice()
function to get the chunk and reverse it
Show Solution
- Get the length of a list using a
len()
function - Divide the length by 3 to get the chunk size
- Run loop three times
- In each iteration, get a chunk using a
slice(start, end, step)
function and reverse it using thereversed()
function - In each iteration,
start
andend
value will change
Exercise 4: Count the occurrence of each element from a list
Write a program to iterate a given list and count the occurrence of each element and create a dictionary to show the count of each element.
Given:
sample_list = [11, 45, 8, 11, 23, 45, 23, 45, 89]
Code language: Python (python)
Expected Output:
Printing count of each item {11: 2, 45: 3, 8: 1, 23: 2, 89: 1}
Show Solution
Exercise 5: Create a Python set such that it shows the element from both lists in a pair
Given:
first_list = [2, 3, 4, 5, 6, 7, 8]
second_list = [4, 9, 16, 25, 36, 49, 64]
Code language: Python (python)
Expected Output:
Result is {(6, 36), (8, 64), (4, 16), (5, 25), (3, 9), (7, 49), (2, 4)}
Show Hint
Use the zip()
function. This function takes two or more iterables (like list, dict, string), aggregates them in a tuple, and returns it.
Show Solution
Exercise 6: Find the intersection (common) of two sets and remove those elements from the first set
See: Python Set
Given:
first_set = {23, 42, 65, 57, 78, 83, 29}
second_set = {57, 83, 29, 67, 73, 43, 48}
Code language: Python (python)
Expected Output:
Intersection is {57, 83, 29} First Set after removing common element {65, 42, 78, 23}
Show Hint
- Use the
intersection()
andremove()
method of a set
Show Solution
- Get the common items using the
first_set.intersection(second_set)
- Next, iterate common items using a for loop
- In each iteration, use the
remove()
method of on first set and pass the current item to it.
Exercise 7: Checks if one set is a subset or superset of another set. If found, delete all elements from that set
Given:
first_set = {27, 43, 34}
second_set = {34, 93, 22, 27, 43, 53, 48}
Code language: Python (python)
Expected Output:
First set is subset of second set - True Second set is subset of First set - False First set is Super set of second set - False Second set is Super set of First set - True First Set set() Second Set {67, 73, 43, 48, 83, 57, 29}
Show Hint
Use the below methods of a set class
issubset()
issuperset()
clear()
Show Solution
Exercise 8: Iterate a given list and check if a given element exists as a key’s value in a dictionary. If not, delete it from the list
Given:
roll_number = [47, 64, 69, 37, 76, 83, 95, 97]
sample_dict = {'Jhon':47, 'Emma':69, 'Kelly':76, 'Jason':97}
Code language: Python (python)
Expected Outcome:
After removing unwanted elements from list [47, 69, 76, 97]
Show Solution
Exercise 9: Get all values from the dictionary and add them to a list but don’t add duplicates
Given:
speed = {'jan': 47, 'feb': 52, 'march': 47, 'April': 44, 'May': 52, 'June': 53, 'july': 54, 'Aug': 44, 'Sept': 54}
Code language: Python (python)
Expected Outcome:
[47, 52, 44, 53, 54]
Show Solution
Exercise 10: Remove duplicates from a list and create a tuple and find the minimum and maximum number
Given:
sample_list = [87, 45, 41, 65, 94, 41, 99, 94]
Code language: Python (python)
Expected Outcome:
unique items [87, 45, 41, 65, 99] tuple (87, 45, 41, 65, 99) min: 41 max: 99
Show Solution
sample_list = [87, 52, 44, 53, 54, 87, 52, 53]
print("Original list", sample_list)
sample_list = list(set(sample_list))
print("unique list", sample_list)
t = tuple(sample_list)
print("tuple ", t)
print("Minimum number is: ", min(t))
print("Maximum number is: ", max(t))
Code language: Python (python)