Registry Synced

Week 8: Comprehensions & Custom Sorting

4411 words
22 min read
2026-09-12

Reading compass

Now · TL;DR

Programming in Python — Week 8: Comprehensions & Custom Sorting

The official IITM syllabus lists Week 8: Basic Collections in Python (Continued). It does not specify finer subtopics, so the material below is a standard continuation of collection-processing concepts, not a claim about exact lecture ordering.

TL;DR

Week 6–7 introduced:
python
list
tuple
string
dictionary
set
Week 8 is where these structures start being combined into actual data-processing algorithms.
The central patterns are:
text
collection
   ↓
traverse
   ↓
filter
   ↓
transform
   ↓
aggregate
   ↓
build another collection
The most useful ideas to master are:
  • nested collections
  • list/dict/set comprehensions
  • zip()
  • enumerate()
  • sorting collections
  • sorting with keys
  • copying and aliasing
  • nested mutation
  • grouping
  • frequency analysis
  • collection conversions
  • structural transformations
  • combined list/dictionary/set problems

1. Collections start becoming composable

Earlier:
python
numbers = [10, 20, 30]
was just a list.
Now we may have:
python
students = {
    "A": [80, 90, 85],
    "B": [70, 60, 75],
    "C": [95, 88, 91]
}
This combines:
  • dictionary
  • string keys
  • list values
  • numerical aggregation
Real programs frequently use such nested structures.

2. List of tuples

Example:
python
records = [
    ("A", 80),
    ("B", 90),
    ("C", 75)
]
Each element is one tuple:
text
(name, mark)
So:
python
records[1]
gives:
python
("B", 90)
and:
python
records[1][0]
gives:
text
"B"

3. Tuple unpacking while iterating

Instead of:
python
for record in records:
    name = record[0]
    mark = record[1]
write:
python
for name, mark in records:
    print(name, mark)
Python assigns the tuple components directly.

4. Structured iteration

For:
python
records = [
    ("A", 80),
    ("B", 90)
]
this:
python
for name, mark in records:
iterates conceptually as:
text
name = "A", mark = 80
name = "B", mark = 90
This is an important pattern.

5. enumerate()

Sometimes you need both:
  • element
  • position
Instead of:
python
for i in range(len(values)):
    print(i, values[i])
Python provides:
python
for i, value in enumerate(values):
    print(i, value)

6. enumerate() example

python
names = ["A", "B", "C"]

for i, name in enumerate(names):
    print(i, name)
Output:
text
0 A
1 B
2 C

7. Starting enumeration from 1

python
for i, name in enumerate(names, start=1):
    print(i, name)
Output:
text
1 A
2 B
3 C
Useful when human numbering starts from 1.

8. zip()

zip() combines multiple sequences position-by-position.
python
names = ["A", "B", "C"]
marks = [80, 90, 70]

for name, mark in zip(names, marks):
    print(name, mark)
Conceptually creates pairs:
text
("A", 80)
("B", 90)
("C", 70)

9. Why zip() is useful

Without it:
python
for i in range(len(names)):
    print(names[i], marks[i])
With it:
python
for name, mark in zip(names, marks):
    print(name, mark)
The second form directly expresses the relationship.

10. Different lengths with zip()

Suppose:
python
a = [1, 2, 3]
b = [10, 20]
Then ordinary:
python
zip(a, b)
stops when the shorter iterable is exhausted.
Conceptually:
text
(1,10)
(2,20)
The third element of a has no partner.

11. Create dictionary using zip()

python
names = ["A", "B", "C"]
marks = [80, 90, 70]

d = dict(zip(names, marks))
Result:
python
{
    "A": 80,
    "B": 90,
    "C": 70
}
This converts parallel sequences into a mapping.

12. Collection conversion

Python can convert between compatible collection forms.
Examples:
python
list("abc")
gives:
python
["a", "b", "c"]
python
tuple([1, 2, 3])
gives:
python
(1, 2, 3)
python
set([1, 2, 2, 3])
gives a set of distinct values.

13. Dictionary conversion

A sequence of pairs can become a dictionary:
python
pairs = [
    ("a", 1),
    ("b", 2)
]

d = dict(pairs)
Result:
python
{"a": 1, "b": 2}

14. List comprehension

Expanded form:
python
squares = []

for x in range(5):
    squares.append(x ** 2)
Comprehension:
python
squares = [x ** 2 for x in range(5)]
Result:
text
[0, 1, 4, 9, 16]

15. Comprehension mental model

Read:
python
[f(x) for x in data]
as:
for every x in data, compute f(x) and collect the results.

16. Filtering comprehension

python
evens = [x for x in range(10) if x % 2 == 0]
Result:
text
[0, 2, 4, 6, 8]
Expanded:
python
evens = []

for x in range(10):
    if x % 2 == 0:
        evens.append(x)

17. Transform + filter

python
result = [x ** 2 for x in values if x > 0]
means:
  1. traverse values
  2. keep only positive values
  3. square them
  4. collect results
Order:
text
filter → transform

18. Conditional expression inside comprehension

python
labels = ["even" if x % 2 == 0 else "odd" for x in values]
This transforms every value.
Compare with:
python
[x for x in values if x % 2 == 0]
which filters elements.
These are different roles for if.

19. Dict comprehension

python
squares = {x: x ** 2 for x in range(5)}
Result:
python
{
    0: 0,
    1: 1,
    2: 4,
    3: 9,
    4: 16
}
General pattern:
python
{key_expression: value_expression for item in collection}

20. Filter dict comprehension

python
high = {
    name: mark
    for name, mark in marks.items()
    if mark >= 80
}
This produces only entries satisfying the condition.

21. Set comprehension

python
remainders = {x % 3 for x in range(10)}
Possible result:
text
{0, 1, 2}
Duplicates collapse because the target collection is a set.

22. Why set comprehension can shrink output

Input may contain many items:
python
[x % 3 for x in range(10)]
produces repeated values.
But:
python
{x % 3 for x in range(10)}
keeps only distinct outcomes.

23. Nested list comprehension

A nested loop:
python
pairs = []

for i in range(3):
    for j in range(2):
        pairs.append((i, j))
can be written:
python
pairs = [(i, j) for i in range(3) for j in range(2)]

24. Order in nested comprehension

Read:
python
[(i, j) for i in range(3) for j in range(2)]
in the same order as:
python
for i in range(3):
    for j in range(2):
        ...
Result:
text
(0,0)
(0,1)
(1,0)
(1,1)
(2,0)
(2,1)

25. Prefer readability over compression

This:
python
[x*y for x in a for y in b if complicated_condition]
may be valid but hard to understand.
Expanded loops are often better when:
  • logic is complex
  • multiple state variables exist
  • debugging matters
Compact code is not automatically better code.

26. Sorting a list

Mutation:
python
values.sort()
Example:
python
values = [5, 2, 9, 1]
values.sort()
Result:
text
[1, 2, 5, 9]

27. Descending order

python
values.sort(reverse=True)
Result:
text
[9, 5, 2, 1]

28. sorted()

python
new_values = sorted(values)
returns a new sorted list.
Original collection need not be modified.
This is often useful if you still need original ordering.

29. Sorting strings

python
names = ["Zara", "Asha", "Ravi"]
print(sorted(names))
uses Python's string ordering.
Output conceptually:
text
["Asha", "Ravi", "Zara"]

30. Sorting tuples

Tuples can be sorted lexicographically.
python
pairs = [(2, 5), (1, 9), (2, 3)]

print(sorted(pairs))
Comparison first uses first element.
For equal first element, next element breaks the tie.
Result:
text
[(1, 9), (2, 3), (2, 5)]

31. Sorting records by chosen field

Suppose:
python
students = [
    ("A", 80),
    ("B", 95),
    ("C", 70)
]
Want sort by mark.
Python:
python
sorted_students = sorted(students, key=lambda x: x[1])
Result:
text
[("C", 70), ("A", 80), ("B", 95)]

32. key= intuition

The key function tells Python:
when comparing elements, use this derived value.
For:
python
key=lambda x: x[1]
each tuple:
python
(name, mark)
is compared using:
text
mark

33. Descending by key

python
sorted_students = sorted(
    students,
    key=lambda x: x[1],
    reverse=True
)
Now highest mark comes first.

34. lambda supporting intuition

A lambda is a small anonymous function.
python
lambda x: x[1]
conceptually means:
python
def f(x):
    return x[1]
For Week 8 purposes, focus on its use as a simple sorting key rather than advanced functional-programming theory.

35. Sort dictionary items

Suppose:
python
marks = {
    "A": 80,
    "B": 95,
    "C": 70
}
Sort by value:
python
sorted_items = sorted(
    marks.items(),
    key=lambda pair: pair[1]
)
Result:
text
[("C",70), ("A",80), ("B",95)]

36. Dictionary retains mapping, sorted result is a list

Important:
python
sorted(marks.items())
returns a list of tuples, not automatically a dictionary.
Always track the datatype produced by an operation.

37. reversed()

Python provides:
python
reversed(values)
which gives an iterator over values in reverse order.
Often:
python
list(reversed(values))
is used if an actual list is needed.

38. Mutation vs returned result recap

Mutates

python
x.append(...)
x.extend(...)
x.sort()
x.reverse()

Returns a new useful value

python
sorted(x)
list(reversed(x))
x + y
x[:]
This distinction is central to collection reasoning.

39. Shallow copies and nested lists

Consider:
python
a = [1, 2], [3, 4](/viewer?path=1, 2], [3, 4)
b = a.copy()
a and b are different outer lists.
But their inner lists are still shared.

40. Shallow-copy example

python
a = [1, 2], [3, 4](/viewer?path=1, 2], [3, 4)
b = a.copy()

b[0][0] = 99
Now both may show:
text
[99, 2], [3, 4](/viewer?path=99, 2], [3, 4)
because the inner list object was shared.

41. Why this happens

Conceptually:
text
a ──> outer list A ──┐
                     ├─> inner list [1,2]
b ──> outer list B ──┘
Only the outer list was copied.
Nested mutable objects were not recursively duplicated.

42. Replacing inner list behaves differently

python
b[0] = [9, 9]
changes which inner list b points to at index 0.
That does not necessarily alter:
python
a[0]
because the outer lists themselves are different.

43. Nested aliasing is a common exam trap

Distinguish:
python
b[0] = ...
from:
python
b[0][0] = ...
The first replaces an inner-object reference in b.
The second mutates the shared inner object.

44. Creating a matrix correctly

Potentially dangerous:
python
matrix = [[0] * 3] * 3
This can create repeated references to the same inner list.

45. Example of shared-row issue

python
matrix = [[0] * 3] * 3

matrix[0][0] = 1
You may unexpectedly get:
text
[
 [1,0,0],
 [1,0,0],
 [1,0,0]
]
because all rows reference the same inner list.

46. Safer matrix construction

Use:
python
matrix = [[0 for _ in range(3)] for _ in range(3)]
Now each row is a distinct list.
This is a major nested-mutable-object lesson.

47. Matrix traversal

python
for row in matrix:
    for value in row:
        print(value)
This is direct value traversal.

48. Matrix traversal with indices

python
for i in range(len(matrix)):
    for j in range(len(matrix[i])):
        print(i, j, matrix[i][j])
Use this when positions matter.

49. Row sums

python
row_sums = []

for row in matrix:
    row_sums.append(sum(row))
Or:
python
row_sums = [sum(row) for row in matrix]

50. Column sums

For a rectangular matrix:
python
rows = len(matrix)
cols = len(matrix[0])

column_sums = []

for j in range(cols):
    total = 0

    for i in range(rows):
        total += matrix[i][j]

    column_sums.append(total)

51. Transpose intuition

Transpose swaps rows and columns.
If:
AijA_{ij}
is an entry, transpose uses:
AjiT=Aij.A^T_{ji}=A_{ij}.
Python nested-list version:
python
transpose = []

for j in range(cols):
    row = []

    for i in range(rows):
        row.append(matrix[i][j])

    transpose.append(row)

52. Nested comprehension transpose

python
transpose = [
    [matrix[i][j] for i in range(rows)]
    for j in range(cols)
]
Expand it mentally if tracing becomes difficult.

53. List of dictionaries

Very common structured-data form:
python
students = [
    {"name": "A", "mark": 80},
    {"name": "B", "mark": 92},
    {"name": "C", "mark": 70}
]
Each list element is one record.

54. Traverse records

python
for student in students:
    print(student["name"], student["mark"])
This resembles a table:
namemark
A80
B92
C70

55. Filter records

python
high = []

for student in students:
    if student["mark"] >= 80:
        high.append(student)
Result contains whole records satisfying the condition.

56. Extract one field

python
names = [student["name"] for student in students]
Result:
text
["A", "B", "C"]

57. Aggregate records

Average mark:
python
total = 0

for student in students:
    total += student["mark"]

average = total / len(students)
This is data-table-style processing.

58. Find record with maximum field

python
best = students[0]

for student in students[1:]:
    if student["mark"] > best["mark"]:
        best = student
At end, best contains the entire record, not only the maximum number.

59. Why keeping whole record matters

If you only store:
python
maximum = 92
you lose associated information such as:
text
who scored 92?
Keeping the record preserves context.

60. Dictionary of records

Another organization:
python
students = {
    "A": {"mark": 80, "city": "X"},
    "B": {"mark": 92, "city": "Y"}
}
Now name is the dictionary key.
Lookup:
python
students["B"]["mark"]
gives:
text
92

61. Choosing list-of-dicts vs dict-of-dicts

List of dictionaries

Useful when primarily processing all records sequentially.

Dictionary keyed by ID/name

Useful when frequent direct lookup by unique identifier matters.
Data structure should reflect the access pattern.

62. Grouping records

Suppose:
python
records = [
    ("A", "Maths"),
    ("B", "Python"),
    ("A", "Python")
]
Want:
python
{
    "A": ["Maths", "Python"],
    "B": ["Python"]
}
Algorithm:
python
groups = {}

for student, course in records:
    if student not in groups:
        groups[student] = []

    groups[student].append(course)

63. Group into sets instead

If duplicate courses should not repeat:
python
groups = {}

for student, course in records:
    if student not in groups:
        groups[student] = set()

    groups[student].add(course)
Now each student's courses are unique.

64. Frequency + maximum

Suppose:
python
values = ["a", "b", "a", "c", "a", "b"]
First build frequency:
python
freq = {}

for x in values:
    freq[x] = freq.get(x, 0) + 1
Then find most frequent:
python
best = None

for x in freq:
    if best is None or freq[x] > freq[best]:
        best = x
Result:
text
"a"

65. Mode

The most frequent value is often called the mode.
Dictionary frequency counting is a natural algorithm for finding it.
Be careful with ties: a dataset can have multiple values sharing maximum frequency.

66. Handling ties

python
max_freq = max(freq.values())

modes = []

for value, count in freq.items():
    if count == max_freq:
        modes.append(value)
Now every value tied for highest frequency is retained.

67. Inverting a dictionary safely

Suppose:
python
d = {
    "A": 1,
    "B": 2,
    "C": 1
}
Naive inversion loses data.
Better:
python
reverse = {}

for key, value in d.items():
    if value not in reverse:
        reverse[value] = []

    reverse[value].append(key)
Result:
python
{
    1: ["A", "C"],
    2: ["B"]
}

68. This is another grouping problem

Original:
text
key → value
Reverse safely:
text
value → list of keys
This generalizes many data-transformation tasks.

69. Set operations in data comparison

Suppose:
python
python_students = {"A", "B", "C"}
maths_students = {"B", "C", "D"}
Both courses:
python
python_students & maths_students
gives:
text
{"B", "C"}

70. Either course

python
python_students | maths_students
gives all students in at least one course.

71. Python only

python
python_students - maths_students
gives students in Python but not Maths.

72. Exactly one course

python
python_students ^ maths_students
gives students enrolled in exactly one of the two.
This is Boolean-style reasoning applied to sets.

73. Any / all intuition

Python has:
python
any(...)
all(...)
for Boolean collections/iterables.
Example:
python
values = [5, 8, 2]

any(x > 7 for x in values)
asks:
Does at least one value exceed 7?
Result:
text
True

74. all()

python
all(x > 0 for x in values)
asks:
Are all values positive?
For:
python
[5, 8, 2]
result:
text
True

75. Existential and universal patterns

Earlier manual pattern:
python
found = False

for x in values:
    if condition:
        found = True
        break
corresponds conceptually to:
python
any(condition for x in values)
Similarly, universal checking maps naturally to all().

76. all() on an empty collection

Mathematically/Pythonically:
python
all([])
is:
text
True
while:
python
any([])
is:
text
False
This follows the logical identities:
  • no counterexample exists in an empty set → universal condition is vacuously true
  • no witness exists → existential condition is false

77. min() and max() with key

Suppose:
python
students = [
    {"name": "A", "mark": 80},
    {"name": "B", "mark": 92}
]
You can write:
python
best = max(students, key=lambda s: s["mark"])
Result is the whole dictionary record with greatest mark.

78. Why key= is powerful

Without key, Python compares objects according to their normal comparison rules.
With:
python
key=...
you explicitly define the feature relevant to the ordering.
This is similar to:
sort/classify records by a chosen attribute.

79. Multiple sort criteria

Tuples can encode tie-breaking.
Suppose rank by:
  1. higher marks
  2. alphabetically smaller name
Conceptually, sorting logic may use a composite key.
The exact key expression can vary, but the algorithmic lesson is:
primary criterion→tie-break criterion\boxed{ \text{primary criterion} \rightarrow \text{tie-break criterion} }

80. Stable sorting intuition

Python sorting is stable: items that compare equal under the sorting key retain their relative order.
This allows multi-stage sorting strategies.
Treat this as useful Python behavior rather than the central Week 8 syllabus point.

81. String splitting

Collections and strings often interact through:
python
text.split()
Example:
python
text = "one two three"
words = text.split()
Result:
python
["one", "two", "three"]
Now string data becomes a list.

82. Split by delimiter

python
"a,b,c".split(",")
gives:
python
["a", "b", "c"]
Useful for simple structured text.

83. Joining strings

The inverse-style operation:
python
",".join(["a", "b", "c"])
gives:
text
"a,b,c"
Pattern:
text
list of strings
→ join
→ one string

84. Word frequency

python
text = "cat dog cat bird dog cat"
words = text.split()

freq = {}

for word in words:
    freq[word] = freq.get(word, 0) + 1
Final:
python
{
    "cat": 3,
    "dog": 2,
    "bird": 1
}
This combines strings, lists and dictionaries.

85. Normalize before counting

Suppose text contains:
text
Cat
cat
CAT
Without normalization, they are distinct strings.
A common step:
python
word = word.lower()
before frequency counting.
Data cleaning often precedes aggregation.

86. Nested structure trace

python
d = {
    "A": [1, 2],
    "B": [3]
}

d["A"].append(4)
d["B"][0] = 9
Final:
python
{
    "A": [1, 2, 4],
    "B": [9]
}
Trace from outer lookup to inner mutation.

87. Shared nested values

Consider:
python
x = [1, 2]
d = {
    "A": x,
    "B": x
}
Both dictionary keys reference the same list.
Then:
python
d["A"].append(3)
also affects the list observed through:
python
d["B"]
Aliasing applies inside dictionaries too.

88. Equality vs identity revisited

Two lists can have equal contents:
python
a = [1, 2]
b = [1, 2]
So:
python
a == b
is true.
But they may be different objects.
Collection algorithms usually care about equality of contents, while mutation/aliasing questions care about identity/reference relationships.

Common mistake: nested-list multiplication aliasing
Avoid:
python
rows = [[0] * 4] * 3
when you need independent mutable rows.
Prefer:
python
rows = [[0] * 4 for _ in range(3)]

Common mistake: assuming comprehension changes original
python
y = [x * 2 for x in values]
creates a new list.
It does not normally mutate values.

Common mistake: expecting set order
Do not index or rely on a set's traversal order for semantic correctness.
If sorted output is required:
python
sorted(s)
returns a list in sorted order.

Common mistake: sorting dictionary directly by values
python
sorted(d)
sorts dictionary keys, because iterating over a dictionary yields keys.
To sort by values, operate on:
python
d.items()
with a suitable key.

Common mistake:
zip() fills missing values
Ordinary zip() does not.
It stops at the shortest iterable.
So mismatched lengths may silently omit extra elements.

Common mistake:
enumerate() gives value first
Standard unpacking:
python
for index, value in enumerate(values):
not:
python
for value, index in enumerate(values):
unless you intentionally want variables misnamed.

Common mistake: losing associated data
Suppose you need:
student with highest mark
If you calculate only:
python
max(marks)
you may lose which student produced that maximum.
Keep the relationship via tuples/dictionaries/records.

Common mistake: overwrite a group
Wrong:
python
groups[key] = value
when multiple values may belong to the same key.
Better:
python
if key not in groups:
    groups[key] = []

groups[key].append(value)

Common mistake: mutate shared nested object unexpectedly
Before modifying:
python
collection[a][b]
ask:
Could this inner object also be referenced somewhere else?
This is the main nested-aliasing question.

98. Data-pipeline mental model

By Week 8, a large fraction of basic Python problems fit:
text
RAW DATA
   ↓
parse / convert
   ↓
collection
   ↓
filter
   ↓
transform
   ↓
group
   ↓
aggregate
   ↓
sort
   ↓
output
That same pipeline becomes central later in data science.

99. Example full pipeline

Input:
python
records = [
    ("A", 80),
    ("B", 45),
    ("C", 90),
    ("D", 75)
]
Goal:
names of students scoring at least 60, highest first.
python
passed = []

for name, mark in records:
    if mark >= 60:
        passed.append((name, mark))

passed = sorted(
    passed,
    key=lambda x: x[1],
    reverse=True
)

names = [name for name, mark in passed]
Result:
text
["C", "A", "D"]

100. Combined frequency pipeline

Input:
python
words = ["a", "b", "a", "c", "b", "a"]
Frequency:
python
freq = {}

for word in words:
    freq[word] = freq.get(word, 0) + 1
Sort by descending count:
python
ranking = sorted(
    freq.items(),
    key=lambda item: item[1],
    reverse=True
)
Result conceptually:
text
[("a",3), ("b",2), ("c",1)]

101. Week 8 synthesis

You should now see collections less as isolated syntax:
text
list
dict
set
tuple
and more as tools answering different questions:
text
Need ordered sequence?
→ list

Need fixed structured record?
→ tuple

Need key-based lookup/grouping?
→ dictionary

Need uniqueness/membership?
→ set

102. Compact Week 8 cheat sheet

python
## index + value
for i, x in enumerate(values):
    ...

## parallel iteration
for a, b in zip(xs, ys):
    ...

## list comprehension
[x * 2 for x in values]

## filtered comprehension
[x for x in values if x > 0]

## dict comprehension
{x: x ** 2 for x in values}

## set comprehension
{x % 3 for x in values}

## sorting
sorted(values)
sorted(values, reverse=True)

## sort records by field
sorted(records, key=lambda x: x[1])

## split string
words = text.split()

## join strings
text = " ".join(words)

## existential / universal
any(condition for x in values)
all(condition for x in values)

103. What you should actually master

By the end of Python Week 8, you should be able to:
  1. Work with nested collections.
  2. Iterate through tuples using unpacking.
  3. Use enumerate() when both index and value matter.
  4. Use zip() for parallel sequences.
  5. Convert between common collection types.
  6. Write and mentally expand list comprehensions.
  7. Use filters in comprehensions.
  8. Understand dict and set comprehensions.
  9. Sort lists and records.
  10. Use key= for sorting by a chosen attribute.
  11. Distinguish mutation from returned new collections.
  12. Understand shallow copies.
  13. Trace nested aliasing.
  14. Construct independent nested lists correctly.
  15. Traverse matrices and nested records.
  16. Group data into lists/sets using dictionaries.
  17. Build and analyze frequency maps.
  18. Find modes and handle ties.
  19. Compare groups using set operations.
  20. Use any() and all() conceptually.
  21. Split strings into lists and join lists into strings.
  22. Build multi-stage collection-processing pipelines.
  23. Choose the correct collection based on access pattern and required semantics.
If these are comfortable, Programming in Python Week 8 is under control.

Quick Recall & Flashcards

Test your active recall before proceeding to practice problems. Format:
Question :: Answer.
  • Week 8 Diagnostic: What does the list comprehension [x * 2 for x in range(5) if x % 2 == 0] evaluate to? :: Answer: A — Even numbers in range(5): 0, 2, 4. Multiplied by 2: 0, 4, 8.
  • Week 8 Diagnostic: What does sorted(['banana', 'pie', 'apple'], key=len) sort the list by? :: Answer: B — Passing key=len orders items by the result of len(item): ['pie', 'apple', 'banana'].

Practice & Self-Check

Attempt these diagnostic questions to verify your operational understanding.
1

Question 1

What does the list comprehension [x * 2 for x in range(5) if x % 2 == 0] evaluate to?
MCQ
3 Unit Assessment
A. [0, 4, 8]
B. [0, 2, 4]
C. [2, 4, 6]
D. [0, 2, 4, 6, 8]
2

Question 2

What does sorted(['banana', 'pie', 'apple'], key=len) sort the list by?
MCQ
3 Unit Assessment
A. Alphabetical order
B. Length of the string (ascending)
C. Number of vowels
D. Reverse alphabetical order
Document outline

Keep your place and jump directly to a heading.

Table of contents

No note selected

Open a chapter to see its outline, backlinks, and sources here.