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 exercises with solution

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]

num = [22,'PythonLobby',22.3]
print(num)

# printing item with its type
for i in range(len(num)):
print(num[i],type(num[i]))

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

num = [23,24,54,34]
for i in range(len(num)):
    print(num[i], end=" ")

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

num = [23, 34, 'hello', 32, 56]
count = 0
for i in range(len(num)):
    count += 1
print("Total count of items are:",count)

# Approach 2
print("Total count of items are:",len(num))

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]

def rev(num):
    num.reverse()
    return num

num = [23, 34, 'hello', 32, 56]
print(rev(num))

# Approach 2
num2 = [23, 34, 'hello', 32, 56]
print(num2[::-1])

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

num = [2, 3, 4, 5, 6]

for i in range(len(num)):
    x = num[i]*num[i]
    print(x, end=' ')

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]

num = ["Hello", 34, 45, '', 40]
while("" in num):
    num.remove("")

# printing list after removing empty string
print(num)

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

num = [23,45,67,8,9]
x = input("Enter the item to insert: ")
num.append(x)

# printing list after insertion
print(num)

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

num = [3, 5, 7, 9, 23, 15]
for i in range(len(num)):
    if num[i] % 5 == 0:
        print(num[i], end=' ')
    else:
        pass

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

num = [2,3,2,4,7,8]
result = sum(num)

# printing result
print("Sum of list items",result)

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

num = [2,3,2,4,7,8]
result = max(num)

# printing result
print("Max in number in list items is",result)

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]

num = [2,3,4,5,2,6,3,2]
x = []
for i in range(len(num)):
    if num[i] not in x:
        x.append(num[i])
    else:
        pass

# printing result
print(x)

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

# importing random module
import random

num = [2,3,4,5,6,8,9]
result = random.choice(num)

# printing result
print(result)

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]

list1 = [23, 24, 25, 26]
list2 = [27, 28, 29, 30]

for i in range(len(list2)):
    list1.append(list2[i])

# printing list 1
print(list1)

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]

num = [2, 23, 24, 51, 46, 67]
even = []
odd = []

for i in range(len(num)):
    if num[i] % 2 == 0:
        even.append(num[i])
    else:
        odd.append(num[i])

print("Even elements are",even)
print("Odd elements are",odd)

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

num = []
n = int(input("How many elements you want to enter: "))
count = 1
for i in range(n):
    x = input(f"Enter element at index {count}: ")
    count += 1
    num.append(x)

# printing list after item insertion
print(num)

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]

num = [2,3,4,5,2,6,3,2]
x = []
for i in range(len(num)):
    if num[i] not in x:
        x.append(num[i])
    else:
        pass

# printing result
print(x)