MCS-201 · Unit 10

Data Structures and Control Statements in Python

Section 5 of 12

Block 3 · Unit 10Data Structures and Control Statements in Python11 min · Section 5 of 12

10.5Lists

Covers IGNOU MCS-201 §10.7.4, 10.8, 10.8.1

A list is Python’s most used collection: an ordered, changeable sequence that grows and shrinks as needed. It replaces most uses of arrays in C, without fixed sizes or manual memory management.

What a list is

  • Ordered: items keep their positions, indexed from 0.
  • Mutable: items can be changed, added and removed after creation.
  • Heterogeneous: one list can mix types, e.g. ['cat', 87.23, 65, [9, 8, 1]].
  • Duplicates allowed: [1, 1, 2] is fine.

Create lists with brackets ([] is empty) or with list(iterable): list("abc") gives ['a', 'b', 'c'].

Indexing, slicing and operators

Lists support the same indexing and slicing as strings (nums[0], nums[-1], nums[1:3], nums[:]). An index beyond the end raises IndexError: list index out of range. Unlike strings, items can be assigned: nums[1] = 99.

OperationExampleResult
+ join[1, 2] + [3][1, 2, 3]
* repeat['pass'] * 3['pass', 'pass', 'pass']
== compare[1, 2] == [1, 2]True
in / not in2 in [1, 2, 3]True

List methods

MethodEffectReturns
append(x)Add x at the endNone
extend(iterable)Add every item of the iterable at the endNone
insert(i, x)Insert x before index i (at the end if i is past the end)None
remove(x)Remove the first x (ValueError if absent)None
pop() / pop(i)Remove and return the last item / item at iThe removed item
index(x)Position of the first x (ValueError if absent)An int
count(x)Number of times x occursAn int
sort()Sort in place (reverse=True for descending)None
reverse()Reverse in placeNone
copy()Shallow copyA new list
clear()Remove all itemsNone

del nums[i] and del nums[i:j] delete by position. Built-in functions that accept lists include len(), sum(), min(), max(), sorted() (returns a new sorted list), any() (true if at least one item is truthy) and all() (true if every item is truthy).

Aliasing versus copying

This is called aliasing: two names referring to one mutable object.

Nested lists

A list can contain lists, which gives a table (a matrix): asia[0][1] is row 0, column 1.

Key takeaways

  • Lists are ordered, mutable, heterogeneous and allow duplicates.
  • Index and slice like strings; assign to items to change them.
  • In-place methods (append, sort, reverse, remove) return None; sorted() returns a new list.
  • Use .copy() or [:] to copy; plain assignment aliases.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What is [10, 20, 30, 40][-2]?

Show answer

Answer: B. 30

−1 is the last item (40) and −2 the one before it (30).

Question 2 · multiple choice

What does [1, 2] * 2 + [3] give?

Show answer

Answer: C. [1, 2, 1, 2, 3]

* repeats the list ([1, 2, 1, 2]), then + joins [3].

Question 3 · multiple choice

What does this program print?

Python · quiz_alias_list.py
a = [1, 2, 3]
b = a
c = a[:]
b.append(4)
print(len(a), len(c))
Show answer

Answer: D. 4 3

b is an alias of a, so appending through b changes a (4 items); c is a separate copy made before the change (3 items).

Question 4 · multiple choice

Which method removes and returns an item?

Show answer

Answer: B. pop()

pop() returns the item it removes; remove() returns None, and del is a statement.

Question 5 · multiple choice

What does this program print?

Python · quiz_remove.py
items = [5, 3, 5, 1]
items.remove(5)
items.insert(1, 9)
print(items)
Show answer

Answer: A. [3, 9, 5, 1]

remove(5) deletes only the first 5, leaving [3, 5, 1]; insert(1, 9) puts 9 before index 1.

Question 6 · short answer

Explain the difference between numlist.sort() and sorted(numlist), with an example.

Show worked answer

numlist.sort() is a list method that rearranges the original list in place and returns None. sorted(numlist) is a built-in function that leaves the original unchanged and returns a new sorted list; it also works on any iterable (tuples, strings, sets, dictionary keys). Example: with n = [3, 1, 2], s = sorted(n) gives s = [1, 2, 3] while n stays [3, 1, 2]; after n.sort(), n itself becomes [1, 2, 3].

↑ ↓ move↵ openTab switch type