Computer Science
Grade 12
20 min
Lists in Python
Lists in Python
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Analyze the time complexity (Big O notation) of common list operations and algorithms.
Construct complex single-line list comprehensions with nested loops and conditional logic.
Utilize lambda functions with higher-order functions like `map()`, `filter()`, and `sorted()` to process list data.
Differentiate between shallow and deep copies of lists and predict their behavior in programs.
Explain the memory implications of list growth and how Python's dynamic array implementation works.
Refactor traditional for-loops into more efficient and Pythonic list-based solutions.
How does a platform like Instagram instantly filter millions of photos by a specific tag? 📸 It all comes down to mastering high-performance operations on lists of data.
You've ma...
2
Key Concepts & Vocabulary
TermDefinitionExample
List ComprehensionA concise, syntactic construct for creating a list based on existing iterables. It provides a more readable and often faster alternative to using for-loops and `append()`.`squares = [x**2 for x in range(10) if x % 2 == 0]` creates a list of squares of even numbers from 0 to 9: `[0, 4, 16, 36, 64]`.
Lambda FunctionA small, anonymous function defined with the `lambda` keyword. It can take any number of arguments but can only have one expression, which is evaluated and returned.`add = lambda x, y: x + y` creates a function that adds two numbers. It's often used inside other functions like `sorted(data, key=lambda item: item['price'])`.
Shallow CopyCreates a new list object, but inserts references to the objects found in the original list...
3
Core Syntax & Patterns
List Comprehension Syntax
`new_list = [expression for item in iterable if condition]`
This is the fundamental pattern for building lists. The `if condition` part is optional and used for filtering elements. The `expression` is what is evaluated and placed into the new list for each `item` that satisfies the condition.
Functional Programming Pattern with Lists
`result = function(lambda arguments: expression, list_data)`
Used with higher-order functions like `map()`, `filter()`, and `sorted(key=...)`. A lambda function is passed as an argument to define the transformation, filtering logic, or sorting key on-the-fly without defining a separate named function.
Copying Rule for Mutable Objects
Assignment (`b = a`) creates a reference. Slicing (`b = a[:]`) or `.copy()` creat...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
Using the `students` data from the tutorial, which line of code produces a list of student names for students with grades of 90 or higher, sorted first by grade descending, then by name alphabetically?
`students = [{'name': 'Alice', 'grade': 88}, {'name': 'Bob', 'grade': 95}, {'name': 'Diana', 'grade': 95}, {'name': 'Charlie', 'grade': 72}]`
A.`[s['name'] for s in sorted([s for s in students if s['grade'] >= 90], key=lambda s: (-s['grade'], s['name']))]`
B.`[s['name'] for s in sorted(students, key=lambda s: s['grade'] >= 90)]`
C.`sorted([s['name'] for s in students if s['grade'] >= 90])`
D.`[s['name'] for s in sorted(students, key=lambda s: (s['grade'], s['name']), reverse=True)]`
Challenging
You have `data = [{'id': 'A', 'values': [10, 20, -5]}, {'id': 'B', 'values': [30, -15, 40]}]`. Which is the most Pythonic one-line solution to produce a flat list of all positive values from all dictionaries?
A.`[val for d in data for val in d['values'] if val > 0]`
B.`list(map(lambda d: d['values'], filter(lambda val: val > 0, data)))`
C.`positive = []; for d in data: for val in d['values']: if val > 0: positive.append(val)`
D.`[val for val in data['values'] if val > 0]`
Challenging
The code `numbers = [1, 2, 3, 4]; for num in numbers: numbers.append(num * 2)` results in an infinite loop. Why does this happen?
A.The list's memory is corrupted by appending while iterating.
B.The iterator never terminates because new items are continuously added to the end of the list it is iterating over.
C.Python's garbage collector fails to reclaim memory, causing a memory leak that freezes the program.
D.The `for` loop in Python automatically resets to the beginning whenever the iterated list is modified.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free