0
0
Pythonprogramming~20 mins

Searching and counting elements in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Master of Searching and Counting
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Counting occurrences in a list
What is the output of this code that counts how many times the number 3 appears in the list?
Python
numbers = [1, 3, 5, 3, 7, 3, 9]
count_3 = numbers.count(3)
print(count_3)
A4
B2
C3
D1
Attempts:
2 left
๐Ÿ’ก Hint
Use the list method that counts how many times an item appears.
โ“ Predict Output
intermediate
2:00remaining
Finding the index of an element
What will be printed by this code that finds the first position of the string 'apple' in the list?
Python
fruits = ['banana', 'apple', 'orange', 'apple']
index = fruits.index('apple')
print(index)
A0
B1
C3
D2
Attempts:
2 left
๐Ÿ’ก Hint
The index method returns the first position where the item is found.
โ“ Predict Output
advanced
2:00remaining
Counting elements with a condition
What is the output of this code that counts how many numbers in the list are greater than 5?
Python
nums = [2, 7, 4, 9, 5, 8]
count = sum(1 for n in nums if n > 5)
print(count)
A3
B4
C2
D5
Attempts:
2 left
๐Ÿ’ก Hint
Sum 1 for each number that meets the condition n > 5.
โ“ Predict Output
advanced
2:00remaining
Finding the first matching element with next()
What will this code print? It finds the first even number in the list or returns -1 if none found.
Python
numbers = [1, 3, 5, 8, 10]
first_even = next((x for x in numbers if x % 2 == 0), -1)
print(first_even)
A8
B10
C-1
D5
Attempts:
2 left
๐Ÿ’ก Hint
next() returns the first item from the generator that meets the condition.
โ“ Predict Output
expert
2:00remaining
Counting unique elements with a condition
What is the output of this code that counts how many unique words in the list have length greater than 3?
Python
words = ['cat', 'dog', 'elephant', 'dog', 'bird', 'cat', 'elephant']
unique_words = set(words)
count = sum(1 for w in unique_words if len(w) > 3)
print(count)
A1
B3
C4
D2
Attempts:
2 left
๐Ÿ’ก Hint
Convert list to set to get unique words, then count those longer than 3 letters.