List Exercises in Python with solution
List Exercises in Python: List is a data type used in python for storing multiple items of different or same data type in a single variable. Other programming languages use arrays for this approach.
These programming exercises have been structured and designed in a beginner-friendly way, focusing on the core concepts of list data type operations.
The main idea behind solving these questions is to make your concept clearer and improve logical thinking of approaching a problem. We are going to cover list operations using some mix of conditional statements i.e if, if-else, for loop(), range(), while loop() etc.
For reference
Python List Exercises for beginners with Solution:
We will solve 15 python list exercises for beginners in python with a solution & detailed code explanation.
Exercise 1: Write a program to create a list with random data types elements.
Hint
List should look like
[22, ‘PythonLobby’, 22.3]
Exercise 2: Write a program to print all the elements of a list in single line.
Hint
num = [23,24,54,34]
Expected output
23 24 54 34
Exercise 3: Write a program to count the number of items stored in a list.
Hint
num = [23, 34, ‘hello’, 32, 56]
Expected output
Total count of items are: 5
Exercise 4: Write a program to reverse a list in Python.
Hint
num = [23, 34, ‘hello’, 32, 56]
Expected output
[56, 32, ‘hello’, 34, 23]
Exercise 5: Python program to square each element of a list.
Hint
Given x = [2, 3, 4, 5, 6]
Expected output
Result : 4 9 16 25 36
Exercise 6: Python program to remove an empty element from a list.
Hint
num = [“Hello”, 34, 45, ””, 40]
Expected output
Result : [‘Hello’, 34, 45, 40]
Exercise 7: Python program to append an element to a list.
Hint
Given num = [23,45,67,8,9]
Input:
Enter the item to insert: study
Expected output
Result: [23,45,67,8,9, ‘study’]
Exercise 8: Write a program to display those items from a list that is divisible by 5.
Hint
Given num = [3, 5, 7, 9, 23, 15]
Expected output
Result: 5 15
Exercise 8: Write a program to sum all the elements of a list.
Hint
Given num = [2,3,2,4,7,8]
Expected output
Sum of list items 26
Exercise 9: Write a program to get the maximum number from a list.
Hint
Given num = [2,3,2,4,7,8]
Expected output
Max number in list items is 8
Exercise 10: Write a program in Python to remove duplicate items from a list.
Hint
Given num = [2,3,4,5,2,6,3,2]
Expected output
Result: [2, 3, 4, 5, 6]
Exercise 11: Write a program in Python to choose a random item from a list.
Hint
Given num = [2,3,4,5,6,8,9]
Expected output
Result: 6
Exercise 12: Write a program to append data of the second list to the first list.
Hint
Given list1 = [23, 24, 25, 26]
list2 = [27, 28, 29, 30]
Expected output
Result:
[23, 24, 25, 26, 27, 28, 29, 30]
Exercise 13: Write a program in Python to filter odd and even number from a list.
Hint
Given [2, 23, 24, 51, 46, 67]
Expected output
Even [2, 24, 46]
Odd [23, 51, 67]
Exercise 14: Write a program to enter or append n numbers in a list.
Hint
Input: 2
Enter element at index 1: 2
Enter element at index 2: 4
Expected output
Result: [‘2’, ‘4’]
Exercise 15: Write a program in Python to remove repetitive items from a list.
Hint
Given num = [2,3,4,5,2,6,3,2]
Expected output
Result: [2, 3, 4, 5, 6]