This page contains 30+ unique practice assignments covering all list concepts in Python. Unlike the topic-specific pages, these problems are designed to test your ability to choose the right list technique for each situation and combine list operations effectively.
š¢ Beginnerš Intermediateš“ Advanced
šÆ Unique Challenge: Each problem is designed to be different from the topic-specific assignments. You'll need to decide which list technique (methods, slicing, comprehension, etc.) is most appropriate for each scenario.
š” Tip: Try each problem yourself first. Click "Show Solution" only after attempting.
1. List Creation Challenges 4 Questions
šÆ Focus: Create lists using different techniques.
Beginner Q1: Create List from Range
Create a list containing all even numbers from 2 to 20 using the range function.
š Explanation: zip() combines iterables, list() converts to list of tuples.
Advanced Q4: Matrix Creation
Create a 3x3 matrix (list of lists) with values 1-9 using nested loops.
š” Solution:
matrix = []
value = 1
for i in range(3):
row = []
for j in range(3):
row.append(value)
value += 1
matrix.append(row)
print(matrix) # [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
š Explanation: Nested loops to create a 2D list.
2. Access & Slicing Challenges 4 Questions
šÆ Focus: Access and slice lists effectively.
Beginner Q5: First and Last Elements
Given a list, print the first and last elements without using index 0 and -1 directly (use slicing).
š” Solution:
nums = [10, 20, 30, 40, 50]
first = nums[:1][0] # or nums[0:1][0]
last = nums[-1:][0] # or nums[-1:][0]
print(f"First: {first}, Last: {last}")
š Explanation: Slicing with step to get first and last elements.
Beginner Q6: Middle Three Elements
Extract the middle three elements from a list of 7 elements.
š Explanation: Slicing to split and concatenate.
Advanced Q24: Remove Duplicates
Remove duplicates from a list while preserving order.
š” Solution:
def remove_duplicates(lst):
seen = set()
result = []
for item in lst:
if item not in seen:
seen.add(item)
result.append(item)
return result
nums = [1, 2, 3, 2, 1, 4, 5, 4]
print(remove_duplicates(nums)) # [1, 2, 3, 4, 5]
š Explanation: Uses set for tracking and list for order preservation.
Advanced Q25: List Intersection
Find the intersection of two lists (common elements).
š” Solution:
def intersection(lst1, lst2):
return [x for x in lst1 if x in lst2]
a = [1, 2, 3, 4, 5]
b = [3, 4, 5, 6, 7]
print(intersection(a, b)) # [3, 4, 5]
š Explanation: List comprehension with membership test.
Advanced Q26: Partition List
Partition a list into two lists: one with even numbers and one with odd numbers.
š” Solution:
def partition(lst):
evens = [x for x in lst if x % 2 == 0]
odds = [x for x in lst if x % 2 != 0]
return evens, odds
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
evens, odds = partition(nums)
print(f"Evens: {evens}")
print(f"Odds: {odds}")
š Explanation: Two comprehensions for partitioning.
Try It Yourself!
Use the interactive editor below to test your solutions or write your own list code.
Loading Pyodide... 0%
Python Code Editor
======================================== LIST PRACTICE ======================================== Squares: [1, 4, 9, 16, 25] Fruits: ['apple', 'banana', 'cherry', 'date', 'elderberry'] Even numbers: [2, 4, 6, 8, 10] Iterating with index: 0: apple 1: banana 2: cherry 3: date 4: elderberry