Python List Exercises (Loops, Conditions, Operators)
Below are 30 exercise questions to practice Python lists using loops, conditions, and
operators. Each question requires iterating over lists, applying conditions, and using
operators to achieve the desired result.
1. Sum of List Elements
Calculate the sum of all elements in a list of numbers (e.g., [4, 2, 9, 1, 5]).
Use a loop and the + operator. Print "List is empty" if the list is empty.
2. Count Even Numbers
Count how many numbers are even in a list (e.g., [3, 8, 1, 4, 7, 2]). Use a
loop and the % operator. Print the count.
3. Find Maximum Element
4. Find the largest number in a list (e.g., [5, 12, 3, 8, 15, 1]) using a loop and
comparison operators (>). Print the largest number or "List is empty" if
empty.
5. Filter Positive Numbers
Create a new list containing only positive numbers from a list (e.g., [-2, 5, -8,
3, 0, 7, -1]). Use a loop and a condition (>). Print the new list.
6. Reverse List
Create a new list with elements in reverse order from a given list (e.g., [1,
"apple", 3.14, "banana"]) using a loop. Print the reversed list.
7. Check for Specific Element
Check if "cat" exists in a list (e.g., ["dog", "cat", "bird", "fish"]) using a loop
and ==. Print "Found" or "Not found."
8. Count Occurrences
Count how many times "apple" appears in a list (e.g., ["apple", "banana",
"apple", "orange", "apple"]) using a loop and ==. Print the count.
9. Remove Duplicates
Create a new list with unique elements from a list (e.g., [1, 2, 2, 3, 1, 4, 3])
using a loop and conditions. Preserve first appearance order. Print the new
list.
[Link] Adjacent Elements
Swap adjacent elements in a list with even length (e.g., [1, 2, 3, 4, 5, 6])
using a loop. For odd length, leave the last element unchanged. Modify and
print the list.
[Link] Odd and Even Numbers
Create two lists from a list of integers (e.g., [1, 2, 3, 4, 5, 6, 7]): one for odd
numbers, one for even. Use a loop and %. Print both lists.
[Link] List Elements
Calculate the product of all elements in a list (e.g., [2, 3, 4, 1]) using a loop
and *. Print "List is empty" if empty.
[Link] Minimum Element
Find the smallest number in a list (e.g., [7, 3, 9, 2, 12, 1]) using a loop and <.
Print the smallest number or "List is empty" if empty.
[Link] Strings by Length
Create a new list containing only strings with length > 3 from a list (e.g.,
["cat", "dog", "apple", "hi", "banana"]) using a loop and len(). Print the new
list.
[Link] Negative Numbers
Count how many numbers are negative in a list (e.g., [-3, 5, -1, 0, 2, -7])
using a loop and <. Print the count.
[Link] First Even Number
Find the first even number in a list (e.g., [1, 3, 4, 7, 8, 2]) using a loop and %.
Print the number or "No even number found."
[Link] Element Conditionally
Append "pass" to a new list for each number > 50 in a list (e.g., [45, 60, 30,
75, 20, 80]) using a loop and >. Print the new list.
[Link] for Duplicates
Check if a list (e.g., [1, 2, 3, 2, 4]) contains any duplicates using a loop and
conditions. Print "Duplicates found" or "No duplicates."
[Link] Negative Numbers
Replace all negative numbers in a list (e.g., [-2, 3, -5, 7, -1]) with 0 using a
loop and <. Modify and print the list.
[Link] Strings Starting with Vowel
Count strings starting with a vowel (a, e, i, o, u) in a list (e.g., ["apple",
"banana", "egg", "cat", "ice"]) using a loop and conditions. Print the count.
[Link] of Even Numbers
Calculate the sum of only even numbers in a list (e.g., [1, 2, 3, 4, 6, 7]) using
a loop and %. Print the sum.
[Link] List of Squares
Create a new list containing squares of numbers from a list (e.g., [1, 2, 3, 4])
using a loop and *. Print the new list.
[Link] if List is Sorted
Check if a list of numbers (e.g., [1, 2, 3, 4] or [1, 3, 2, 4]) is sorted in
ascending order using a loop and >. Print "Sorted" or "Not sorted."
[Link] Non-Strings
Create a new list containing only non-string elements from a mixed list (e.g.,
[1, "apple", 3.14, "banana", 5]) using a loop and isinstance(). Print the new
list.
[Link] Last Odd Number
Find the last odd number in a list (e.g., [2, 3, 4, 5, 6, 7]) using a loop and %.
Print the number or "No odd number found."
[Link] Strings
Concatenate all strings in a list (e.g., ["hello", "world", "python"]) into a
single string using a loop and +. Print the result or "No strings" if none.
[Link] Elements Greater Than Previous
Count how many elements in a list (e.g., [1, 3, 2, 4, 6, 5]) are greater than
the previous element using a loop and >. Print the count.
[Link] Zeros to End
Move all zeros to the end of a list (e.g., [1, 0, 3, 0, 5, 0]) using a loop and
conditions. Preserve the order of non-zero elements. Modify and print the
list.
[Link] for Palindrome List
Check if a list is a palindrome (e.g., [1, 2, 2, 1] or [1, 2, 3]) using a loop and
==. Print "Palindrome" or "Not palindrome."
[Link] Elements by Type
Create a new list containing only integers from a mixed list (e.g., [1, "apple",
2.5, 3, "banana", 4]) using a loop and isinstance(). Print the new list.
[Link] Elements
Create a new list by alternating elements from two lists of equal length
(e.g., [1, 3, 5] and [2, 4, 6] to get [1, 2, 3, 4, 5, 6]) using a loop. Print the new
list or "Lists must be of equal length" if lengths differ.