Python

P
PythonHub
Progress 0%

Python

  • Home
  • History of Python
  • Applications of Python
  • Introduction To Python
    • What is Python
    • Character Set
    • Tokens in Python
    • Python Execution Mode
    • Variable And Identifiers
    • Data Types in Python
    • Operators And Expressions
    • Constants in Python
    • Assignment Statement
    • Input / Output in Python
    • Simple Python Scripts
    • Namespace in Python
    • šŸ“ Assignments
  • Operators in Python
    • Arithmetic Operators
    • Assignment Operators
    • Shorthand Assignment Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Special Operators
    • šŸ“ Assignments
  • Input Output in Python
    • Accept Input
    • Output Formatting
    • šŸ“ Assignments
  • Conditional Statement
    • Decision Making
    • if Statement
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • Short Hand IF-ELSE
    • šŸ“ Assignments
  • Loops
    • Introduction to Loops
    • While Loop
    • Nested While Loop
    • šŸ“ While Loop Assignments
    • For Loop
    • For Loop Examples
    • Nested For Loop
    • Nested For Loop Examples
    • Infinite While Loops
    • Infinite For Loops
    • Break, Continue and Else in Loops
    • Difference Between For and While Loop
    • For Each Loop
    • šŸ“ For Each Assignments
    • šŸ“ All Loops Assignments
  • List
    • List in Python
    • Access List Elements
    • List Functions
    • Iterate (Loop) List
    • List Comprehension
    • šŸ“ Assignments
  • Tuple
    • Tuple in Python
    • Access Tuple Elements
    • Tuple Functions
    • Iterate (Loop) Tuple
    • Unpack Tuple
    • Tuple Comprehension
    • šŸ“ Assignments
  • Set
    • Set in Python
    • Access Set Elements
    • Set Methods
    • Iterate (Loop) Set
    • Pack/Unpack Set
    • Set Comprehension
    • šŸ“ Assignments
  • Dictionary
    • Dictionary
    • Access Dictionary Items
    • Dictionary Methods
    • Iterate (Loop) Dictionary
    • Formatting Dictionaries
    • Nested Dictionaries
    • Dictionary Comprehension
    • šŸ“ Assignments
  • Diff List Tuple Set Dictionary
    • List vs Tuple
    • List vs Set
    • List vs Dictionary
    • Tuple vs Set
    • Tuple vs Dictionary
    • Dictionary vs Set
    • šŸ“ Assignments
  • Exception
    • Error vs Exception
    • Exception Handling
    • Types of Exception
    • User Defined Exception
    • Logging Exception
    • šŸ“ Assignments
  • Functions
    • Introduction to Functions
    • Modular Programming
    • Types of Functions
    • Inbuilt Functions
    • Need For User-Defined Function
    • Elements of User Defined Function
    • Function Arguments
    • Nesting of Functions
    • Recursion
    • Global Local and Non Local
    • Python Lambda Functions
    • šŸ“ Assignments
  • Python Module
    • Introduction to Module
    • Inbuilt Modules in Python
    • User Defined Module
    • šŸ“ Assignments
  • File Handling
    • Introduction to Files
    • Create File
    • Read Files
    • Write to File
    • Rename File
    • Copy File
    • Move File
    • List Files in Directory
    • Binary Files
    • Zipping and Unzipping Files
    • šŸ“ Assignments
  • Strings
    • Basics of Strings
    • String Special Operators
    • String Formatting Operators
    • String Methods
    • šŸ“ Assignments
  • Regular Expressions
  • Python OOPS
    • Basics of Object Oriented
    • What are Classes and Objects?
    • Creating Class and Object
    • OOP vs Procedural Programming
    • Difference Between Classes and Objects
    • Constructors
    • Destructor
    • Built Class Methods and Attributes
    • Class and Instance Variables
    • Inheritance in Python
    • Single Inheritance
    • Multiple Inheritance
    • Multilevel Inheritance
    • Hierarchical Inheritance
    • Hybrid Inheritance
    • Abstraction
    • Method Overriding
    • Abstract Method
    • Interfaces in Python
    • Abstract Class vs Interface
    • Public, Private and Protected
    • Overloading vs Overriding
    • Inheritance vs Composition
    • Encapsulation
    • Polymorphism
    • Inner Classes
    • šŸ“ Assignments
  • Advanced Python
    • Iterator in Python
    • Generator in Python
    • Decorator in Python
    • Type Hints in Python
    • Async/Await Programming
    • Dataclasses in Python
    • Context Managers in Python
    • Property Decorator in Python
    • __slots__ in Python
    • Enums in Python
    • Walrus Operator in Python
    • Match-Case in Python
    • šŸ“ Assignments
  • Python Standard Library
    • Collections Module
    • Itertools Module
    • Functools Module
    • Datetime Module
    • JSON Module
    • OS Module
    • Sys Module
    • Random Module
    • Math Module
    • šŸ“ Assignments
  • Python Testing
    • Unit Testing in Python
    • Pytest Framework
    • Mocking in Python
    • šŸ“ Assignments
  • Python Best Practices
    • PEP 8 Style Guide
    • Docstrings in Python
    • Logging in Python
    • Code Optimization Tips
    • Debugging Techniques
    • šŸ“ Assignments
  • MySQL Database in Python
    • Introduction to MySQL with Python
    • DBMS vs File System
    • Connecting to MySQL Database
    • Create Database in MySQL
    • Create Table in MySQL
    • Insert Data in MySQL
    • Insert Multiple Rows
    • Select Data from MySQL
    • WHERE Clause in MySQL
    • Update Data in MySQL
    • Delete Data from MySQL
    • Parameterized Queries
    • Transaction Management
    • Error Handling
    • Connection Pooling
    • MySQL Drivers Guide
    • Joins in MySQL
    • Aggregation Functions
    • Backup MySQL Database
    • Best Practices
    • šŸ“ Assignments
  • MySQL Database Operations
    • SELECT Statement
    • MySQL Operators
    • DDL Statements
    • DML Statements
    • Subqueries
    • JOIN Operations
    • Aggregation
    • Case Study
    • šŸ“ Assignments
  • Graphics in Python
  • Threads in Python
    • Introduction to Threads
    • Process vs Threads
    • Concurrent Programming & GIL
    • Uses of Threads
    • Creating Threads
    • Single Tasking
    • Multi Tasking
    • Thread Synchronization
    • šŸ“ Assignments
  • Interview Questions & Answers
  • Python Case Studies
  • Multiple Choice Questions
  • šŸ“ Practice Problems
Get in Touch
  • tech2dsm@gmail.com

© Sankalan Data Tech

Python Language Interactive Tutorial

šŸ“š Python List: Practice Assignments

Python List Practice Assignments

Comprehensive practice covering list creation, access, methods, iteration, comprehension, and more.

Sankalan Data Tech Team Verified
Data Engineers, Analysts, Scientists & Trainers
Created by experienced Python developers to make programming easy through practical examples and clear explanations.
šŸ“‘ On this page:
  • About
  • List Creation
  • Access & Slicing
  • List Methods
  • Iteration
  • List Comprehension
  • Advanced
  • Try It
Share:
Twitter LinkedIn Facebook WhatsApp Reddit Telegram Email Copy Link

About These Assignments

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.
šŸ’” Solution:
evens = list(range(2, 21, 2))
print(evens) # [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
šŸ“– Explanation: range(start, stop, step) with list() constructor.
Beginner Q2: List of Squares
Create a list of squares of numbers from 1 to 10 using a for loop.
šŸ’” Solution:
squares = []
for i in range(1, 11):
squares.append(i ** 2)
print(squares) # [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
šŸ“– Explanation: Uses append() in a loop to build the list.
Intermediate Q3: Zipped List
Create a list of tuples from two separate lists using zip.
šŸ’” Solution:
names = ["Alice", "Bob", "Charlie"]
scores = [85, 92, 78]
combined = list(zip(names, scores))
print(combined) # [('Alice', 85), ('Bob', 92), ('Charlie', 78)]
šŸ“– 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.
šŸ’” Solution:
nums = [5, 10, 15, 20, 25, 30, 35]
middle = nums[2:5]
print(middle) # [15, 20, 25]
šŸ“– Explanation: Slicing from index 2 to 5 (exclusive).
Intermediate Q7: Reverse Using Slicing
Reverse a list using slicing (without using reverse() method).
šŸ’” Solution:
nums = [1, 2, 3, 4, 5]
reversed_nums = nums[::-1]
print(reversed_nums) # [5, 4, 3, 2, 1]
šŸ“– Explanation: Step -1 reverses the list.
Advanced Q8: Every Second Element
Extract every second element from a list starting from index 0.
šŸ’” Solution:
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
every_second = nums[::2]
print(every_second) # [1, 3, 5, 7, 9]
šŸ“– Explanation: Slicing with step 2.

3. List Methods Challenges 5 Questions

šŸŽÆ Focus: Use built-in list methods effectively.
Beginner Q9: Append and Extend
Add elements to a list using append() and extend().
šŸ’” Solution:
fruits = ["apple", "banana"]
fruits.append("cherry")
fruits.extend(["date", "elderberry"])
print(fruits) # ['apple', 'banana', 'cherry', 'date', 'elderberry']
šŸ“– Explanation: append() adds single element, extend() adds multiple.
Beginner Q10: Insert and Remove
Insert an element at position 2 and then remove it using remove().
šŸ’” Solution:
nums = [10, 20, 40, 50]
nums.insert(2, 30)
print(nums) # [10, 20, 30, 40, 50]
nums.remove(30)
print(nums) # [10, 20, 40, 50]
šŸ“– Explanation: insert(index, value) and remove(value).
Intermediate Q11: Pop vs Remove
Remove the last element using pop() and the first occurrence of 5 using remove().
šŸ’” Solution:
nums = [1, 2, 3, 4, 5, 6, 7, 5]
last = nums.pop()
print(f"Popped: {last}, List: {nums}")
nums.remove(5)
print(f"After remove: {nums}")
šŸ“– Explanation: pop() removes by index (default last), remove() by value.
Intermediate Q12: Sort and Reverse
Sort a list in descending order and then reverse it back using sort() and reverse().
šŸ’” Solution:
nums = [5, 2, 8, 1, 9, 3]
nums.sort(reverse=True)
print(nums) # [9, 8, 5, 3, 2, 1]
nums.reverse()
print(nums) # [1, 2, 3, 5, 8, 9]
šŸ“– Explanation: sort(reverse=True) sorts descending, reverse() reverses in-place.
Advanced Q13: Count and Index
Find how many times 3 appears and its first index using count() and index().
šŸ’” Solution:
nums = [1, 3, 5, 3, 7, 3, 9, 3]
count = nums.count(3)
first_index = nums.index(3)
print(f"Count: {count}, First index: {first_index}")
šŸ“– Explanation: count() returns occurrences, index() returns first position.

4. Iteration Challenges 4 Questions

šŸŽÆ Focus: Iterate over lists effectively.
Beginner Q14: Sum with For Loop
Calculate the sum of all elements in a list using a for loop.
šŸ’” Solution:
nums = [10, 20, 30, 40, 50]
total = 0
for num in nums:
total += num
print(f"Sum: {total}")
šŸ“– Explanation: Iterates through each element and accumulates sum.
Beginner Q15: Print Index and Value
Print both index and value for each element using enumerate.
šŸ’” Solution:
fruits = ["apple", "banana", "cherry", "date"]
for index, fruit in enumerate(fruits):
print(f"{index}: {fruit}")
šŸ“– Explanation: enumerate() provides index-value pairs.
Intermediate Q16: Iterate in Chunks
Iterate over a list in chunks of 3 elements.
šŸ’” Solution:
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for i in range(0, len(nums), 3):
chunk = nums[i:i+3]
print(chunk)
šŸ“– Explanation: Slicing with step to get chunks.
Advanced Q17: While Loop Iteration
Iterate over a list using a while loop and modify elements (double each).
šŸ’” Solution:
nums = [1, 2, 3, 4, 5]
i = 0
while i < len(nums):
nums[i] *= 2
i += 1
print(nums) # [2, 4, 6, 8, 10]
šŸ“– Explanation: While loop with index to modify elements in-place.

5. List Comprehension Challenges 5 Questions

šŸŽÆ Focus: Use list comprehension for concise list creation.
Beginner Q18: Squares with Comprehension
Create a list of squares of numbers 1-10 using list comprehension.
šŸ’” Solution:
squares = [x**2 for x in range(1, 11)]
print(squares) # [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
šŸ“– Explanation: [expression for item in iterable].
Beginner Q19: Even Numbers with Comprehension
Create a list of even numbers from 1 to 20 using list comprehension.
šŸ’” Solution:
evens = [x for x in range(1, 21) if x % 2 == 0]
print(evens) # [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
šŸ“– Explanation: With condition [x for x in range if condition].
Intermediate Q20: Nested Comprehension
Flatten a matrix (list of lists) using nested list comprehension.
šŸ’” Solution:
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flat = [num for row in matrix for num in row]
print(flat) # [1, 2, 3, 4, 5, 6, 7, 8, 9]
šŸ“– Explanation: Nested list comprehension with two for clauses.
Intermediate Q21: Conditional with else
Create a list that replaces odd numbers with "odd" and even with "even".
šŸ’” Solution:
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]
result = ["odd" if x % 2 != 0 else "even" for x in nums]
print(result) # ['odd', 'even', 'odd', 'even', 'odd', 'even', 'odd', 'even', 'odd']
šŸ“– Explanation: Ternary operator inside comprehension.
Advanced Q22: Cartesian Product
Create a list of all combinations (cartesian product) of two lists.
šŸ’” Solution:
colors = ["red", "blue"]
sizes = ["S", "M", "L"]
products = [(c, s) for c in colors for s in sizes]
print(products) # [('red', 'S'), ('red', 'M'), ('red', 'L'), ('blue', 'S'), ('blue', 'M'), ('blue', 'L')]
šŸ“– Explanation: Nested loops in comprehension for cartesian product.

6. Advanced List Challenges 4 Questions

šŸŽÆ Focus: Combine multiple list techniques.
Advanced Q23: Rotate List
Rotate a list to the right by k positions.
šŸ’” Solution:
def rotate_list(lst, k):
k = k % len(lst)
return lst[-k:] + lst[:-k]
nums = [1, 2, 3, 4, 5]
print(rotate_list(nums, 2)) # [4, 5, 1, 2, 3]
šŸ“– 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

āœ… Write your list solutions here!

šŸ“š Related Tutorials

šŸ“‹ List in Python

Learn about lists in Python

šŸ”§ List Functions

Explore built-in list methods

⚔ List Comprehension

Master list comprehension

šŸ“– Related Tutorials
  • Access List Elements
  • Iterate List
  • Tuple in Python
  • List vs Tuple
Previous: List Comprehension Next: Tuple in Python →