Python List(advanced)
Table of Contents
Description
While basic list usage covers creation, indexing, and iteration, advanced list concepts include:
.Slicing
.Nested lists
.List methods
.List unpacking
.List comprehension (covered earlier)
.Sorting and reversing
Using lists with functions like map, filter, and zip
These techniques allow more efficient, readable, and Pythonic code.
Prerequisites
- Basic understanding of:
List syntax and indexing
Loops and conditionals
Functions and lambda expressions
Examples
Here's a simple program in Python:
✅ List Slicing my_list = [10, 20, 30, 40, 50] # Get first three elements print(my_list[:3]) # Output: [10, 20, 30] # Get elements in reverse print(my_list[::-1]) # Output: [50, 40, 30, 20, 10] ✅ Nested Lists matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] # Access 2nd row, 3rd column print(matrix[1][2]) # Output: 6 ✅ List Methods nums = [5, 2, 9, 1] # Append an element nums.append(7) # [5, 2, 9, 1, 7] # Remove an element nums.remove(2) # [5, 9, 1, 7] # Sort list nums.sort() # [1, 5, 7, 9] # Reverse list nums.reverse() # [9, 7, 5, 1] ✅ List Unpacking data = [1, 2, 3, 4, 5] # Unpack first and rest first, *rest = data print(first) # 1 print(rest) # [2, 3, 4, 5] ✅ Using map() with Lists nums = [1, 2, 3] # Square each element using map squared = list(map(lambda x: x ** 2, nums)) # [1, 4, 9] ✅ Using filter() with Lists # Keep only even numbers evens = list(filter(lambda x: x % 2 == 0, nums)) # [2] ✅ Using zip() with Lists names = ['Alice', 'Bob'] scores = [85, 92] # Pair names with scores paired = list(zip(names, scores)) # [('Alice', 85), ('Bob', 92)]Real-World Applications
Matrix operations using nested lists
Data transformation pipelines with map/filter
Data visualization input formatting (e.g., zip(x, y))
ETL processes in data science
Configuration/parameter packing via list unpacking
Where topic Can Be Applied
Web scraping: Filtering data from raw HTML
Machine learning: Handling feature vectors
APIs: Unpacking and pairing API response data
UI frameworks: Managing dynamic list-based data rendering
Resources
WatchTopic video source
A comprehensive video
VisitPython pdf
pdf on topic
Interview Questions
What is list slicing and how does it work?
How do you reverse a list in Python?
What is the difference between append() and extend()?
How do you sort a list of dictionaries by a key?
What is list unpacking and when is it useful?
How can you use map() and filter() with lists?