πŸŽ“ Velnov

🐍 Python · Topic 7 of 20

Lists & Tuples β€” Python Questions with Answers

Start with the study notes on Lists & Tuples (definitions, examples and a quick revision), then work through 30 practice questions, each with the correct answer and a clear explanation of why it is right. When you are ready, take this topic as an interactive quiz to test yourself.

12 easy Β· 13 medium Β· 5 hard Β· Part of Python: Master Python from basics to advanced - 20 topics.

Topic overview

Lists and Tuples – ordered collections

1. Lists

A list is an ordered, changeable (mutable) collection, written in square brackets. It can hold any mix of types. Indexing and slicing work just like strings.

fruits = ["apple", "mango", "banana"]
print(fruits[0], fruits[-1])
print(fruits[1:])
print(len(fruits))
fruits[0] = "kiwi"          # lists can be changed
print(fruits)
Outputapple banana
['mango', 'banana']
3
['kiwi', 'mango', 'banana']

2. Adding and removing items

nums = [10, 20, 30]
nums.append(40)
nums.insert(1, 99)
print(nums)
nums.remove(99)
last = nums.pop()
print(nums, last)
del nums[0]
print(nums)
Output[10, 99, 20, 30, 40]
[10, 20, 30] 40
[20, 30]

append() vs extend()

a = [1, 2]
a.append([3, 4])
print(a)
b = [1, 2]
b.extend([3, 4])
print(b)
Output[1, 2, [3, 4]]
[1, 2, 3, 4]

3. Sorting

nums = [5, 2, 8, 1]
print(sorted(nums))
print(nums)
result = nums.sort()
print(result, nums)
nums.sort(reverse=True)
print(nums)
Output[1, 2, 5, 8]
[5, 2, 8, 1]
None [1, 2, 5, 8]
[8, 5, 2, 1]

Remember: x = nums.sort() sets x to None, a very common bug.

4. Useful list operations

nums = [4, 1, 7]
print(max(nums), min(nums), sum(nums))
print(sum(nums) / len(nums))
print(7 in nums)
print([1, 2] + [3, 4], [0] * 3)
print([10, 20, 30, 20].index(20), [10, 20, 30, 20].count(20))
Output7 1 12
4.0
True
[1, 2, 3, 4] [0, 0, 0]
1 2

5. Copying: names vs copies

b = a does not copy a list. Both names refer to the same list, so a change through one shows through the other. Use a.copy(), a[:] or list(a) for an independent copy.

a = [1, 2, 3]
b = a
b.append(4)
print(a)
c = a.copy()
c.append(5)
print(a, c)
Output[1, 2, 3, 4]
[1, 2, 3, 4] [1, 2, 3, 4, 5]

6. Nested lists

A list can contain other lists, like the rows of a table. Index the row first, then the column.

matrix = [[1, 2],
          [3, 4]]
print(matrix[1][0])
Output3

7. Tuples

A tuple is like a list, but immutable: it can't be changed after it is created. It is written with round brackets.

point = (3, 7)
x, y = point          # unpacking
print(x, y)
print(type((5)), type((5,)))   # the comma makes a tuple
Output3 7
<class 'int'> <class 'tuple'>

Remember: point[0] = 9 raises TypeError. A one-item tuple needs a comma: (5,).

When to use a tuple

Use a tuple for fixed data, such as coordinates or RGB colours. Because tuples can't change, they can be dictionary keys, while lists cannot.

places = {(0, 0): "origin", (3, 5): "target"}
print(places[(3, 5)])
print(list("abc"), tuple([1, 2]))
Outputtarget
['a', 'b', 'c'] (1, 2)

8. Common misconceptions

MisconceptionCorrect understanding
b = a makes a copy of the list.Both names point to one list; use a.copy().
nums.sort() returns the sorted list.It sorts in place and returns None; use sorted() for a new list.
(5) is a tuple.It is just the int 5; (5,) is a tuple.
Tuples can be changed like lists.Tuples are immutable.
[1, 2] + [3, 4] adds the numbers.It joins the lists: [1, 2, 3, 4].

9. Quick revision

List
Ordered, mutable collection: [ ].
Tuple
Ordered, immutable collection: ( ).
append()
Adds one item at the end.
extend()
Adds every item from another list.
pop()
Removes and returns an item.
sort() vs sorted()
In place vs returns a new list.
copy()
Makes an independent list.
Unpacking
x, y = point.

10. Test your understanding

  1. What is ["a", "b", "c"][-1]?

    Show answer

    "c"

  2. What is the difference between remove() and pop()?

    Show answer

    remove() deletes by value; pop() deletes by position and returns the item.

  3. What does print(nums.sort()) show?

    Show answer

    None

  4. a = [1, 2]; b = a; b.append(3). What is a?

    Show answer

    [1, 2, 3]

  5. How do you make an independent copy of list a?

    Show answer

    a.copy() (or a[:] or list(a)).

  6. What does [1, 2].append([3]) give?

    Show answer

    [1, 2, [3]], a nested list.

  7. How do you write a one-item tuple?

    Show answer

    (5,)

  8. Why can a tuple be a dictionary key but a list can't?

    Show answer

    Tuples are immutable (hashable); lists are not.

Key takeaway: Lists are flexible and changeable, tuples are fixed; know sort() vs sorted(), append() vs extend(), and that b = a shares one list.

Notes written by Velnov. Every code example with an output was run in Python 3 to check that the output shown is exactly what Python prints.

Key concepts in this topic

ListsTuplesSetsIndexingNegative indexingappend()insert()remove()pop()dellen()ConcatenationRepetitionsort()sorted()sort() vs sorted()In-place methodsDynamic typingNested lists2D dataMutabilityReferencescopy()Aliasing

Jump to: Easy Β· Medium Β· Hard

Easy questions (12)

Q1. easy

Which line creates a list of three numbers?

nums = [1, 2, 3]      # list
point = (1, 2)          # tuple
ids = {1, 2, 3}         # set
  1. A nums = [1, 2, 3]
  2. B nums = (1, 2, 3)
  3. C nums = {1, 2, 3}
  4. D nums = list 1, 2, 3

Answer: A β€” nums = [1, 2, 3]

Square brackets [] create a list. Parentheses () make a tuple and curly braces {} make a set - three different types!

ListsTuplesSets

Q2. easy

What is the output of: fruits = ["apple", "mango", "banana"] print(fruits[1])

fruits = ["apple", "mango", "banana"]
#          0        1        2
print(fruits[1])   # mango
  1. A apple
  2. B mango
  3. C banana
  4. D Error

Answer: B β€” mango

List indexing starts at 0: fruits[0] is 'apple', fruits[1] is 'mango'. Same rule as strings.

Indexing

Q3. easy

What does fruits[-1] give for fruits = ["apple", "mango", "banana"]?

print(fruits[-1])   # banana
print(fruits[-2])   # mango
  1. A apple
  2. B mango
  3. C banana
  4. D Error

Answer: C β€” banana

Negative indexes count from the end: -1 is the LAST item ('banana'), -2 is 'mango'.

Negative indexing

Q4. easy

Which method adds an item to the END of a list?

cart = ["pen"]
cart.append("book")
print(cart)   # ['pen', 'book']
  1. A add()
  2. B append()
  3. C push()
  4. D insert()

Answer: B β€” append()

.append(item) adds one item at the end. (push is JavaScript, add is for sets; insert puts an item at a chosen position.)

append()insert()

Q7. easy

What is the output of: print(len([1, 2, 3, 4]))

  1. A 3
  2. B 4
  3. C 10
  4. D Error

Answer: B β€” 4

len() returns the number of items in the list: 4. Works on strings, tuples, dicts and sets too.

len()

Q8. easy

What is the output of: print([1, 2] + [3, 4])

print([1, 2] + [3, 4])   # [1, 2, 3, 4]
print([0] * 3)            # [0, 0, 0]
  1. A [4, 6]
  2. B [1, 2, 3, 4]
  3. C [[1, 2], [3, 4]]
  4. D Error

Answer: B β€” [1, 2, 3, 4]

+ on lists concatenates them into a new longer list - it does NOT add the numbers element by element. [1,2] + [3,4] -> [1, 2, 3, 4].

ConcatenationRepetition

Q12. easy

Can a Python list hold different types at once, like [1, "two", 3.0, True]?

stuff = [1, "two", 3.0, True, [4, 5]]
print(stuff[4][0])   # 4
  1. A No - all items must share one type
  2. B Yes - lists can mix any types freely
  3. C Only numbers and strings
  4. D Only with the mixed keyword

Answer: B β€” Yes - lists can mix any types freely

Python lists are completely flexible - any mix of types, even other lists. (In practice most lists hold one type, but nothing enforces it.)

Dynamic typingNested lists

Q14. easy

How do you change the first item of nums = [10, 20, 30] to 99?

nums = [10, 20, 30]
nums[0] = 99
print(nums)   # [99, 20, 30]
  1. A nums[0] = 99
  2. B nums.change(0, 99)
  3. C nums = 99
  4. D You can't - lists are immutable

Answer: A β€” nums[0] = 99

Lists ARE mutable - assign directly to an index: nums[0] = 99. (It's tuples and strings that can't be changed.)

MutabilityIndexing

Q18. easy

What is the output of: print(3 in [1, 2, 3])

print(3 in [1, 2, 3])       # True
print(5 not in [1, 2, 3])   # True
  1. A True
  2. B False
  3. C 2
  4. D Error

Answer: A β€” True

in tests membership: is 3 one of the list's items? Yes -> True. Works on lists, tuples, strings, sets and dict keys.

in operatorMembership

Q20. easy

What do max(), min() and sum() return for [4, 1, 7]?

nums = [4, 1, 7]
print(max(nums), min(nums), sum(nums))  # 7 1 12
print(sum(nums) / len(nums))             # 4.0 - average
  1. A 7, 1 and 12
  2. B 7, 4 and 11
  3. C 1, 7 and 12
  4. D Error - they need two arguments

Answer: A β€” 7, 1 and 12

These built-ins take a whole list: max is 7, min is 1, sum is 4+1+7 = 12. Average = sum(nums) / len(nums).

Built-in functionsAverages

Q22. easy

Which line creates a TUPLE?

point = (3, 5)
print(point[0])   # 3 - indexing works like lists
  1. A t = (1, 2, 3)
  2. B t = [1, 2, 3]
  3. C t = {1, 2, 3}
  4. D t = tuple[1, 2, 3]

Answer: A β€” t = (1, 2, 3)

Parentheses create a tuple - an ordered collection like a list, but IMMUTABLE (cannot be changed after creation).

TuplesImmutability

Q27. easy

What does list("abc") return?

print(list("abc"))       # ['a', 'b', 'c']
print(list(range(3)))     # [0, 1, 2]
print(list((1, 2)))       # [1, 2] - tuple to list
  1. A ["abc"]
  2. B ['a', 'b', 'c']
  3. C ("a", "b", "c")
  4. D Error

Answer: B β€” ['a', 'b', 'c']

list() converts any iterable to a list - for a string, each CHARACTER becomes an item: ['a', 'b', 'c'].

list()Type conversion

Medium questions (13)

Q5. medium

What does nums.insert(1, 99) do to nums = [10, 20, 30]?

nums = [10, 20, 30]
nums.insert(1, 99)
print(nums)   # [10, 99, 20, 30]
  1. A Replaces 20 with 99 -> [10, 99, 30]
  2. B Inserts 99 AT index 1 -> [10, 99, 20, 30]
  3. C Appends 99 -> [10, 20, 30, 99]
  4. D Error

Answer: B β€” Inserts 99 AT index 1 -> [10, 99, 20, 30]

insert(position, item) squeezes the item in at that index, shifting the rest right - nothing is overwritten: [10, 99, 20, 30].

insert()append()

Q6. medium

What is the difference between remove() and pop() on a list?

nums = [10, 20, 30]
nums.remove(20)      # by value -> [10, 30]
last = nums.pop()    # by position -> last = 30, nums = [10]
  1. A No difference
  2. B remove(x) deletes by VALUE; pop(i) deletes by POSITION and returns the item
  3. C pop() deletes by value; remove() by position
  4. D remove() empties the whole list

Answer: B β€” remove(x) deletes by VALUE; pop(i) deletes by POSITION and returns the item

nums.remove(20) searches for the value 20 and deletes its first occurrence. nums.pop(1) removes whatever sits at index 1 AND hands it back to you. pop() with no argument removes the last item.

remove()pop()del

Q9. medium

What is the output of: nums = [5, 2, 8, 1] nums.sort() print(nums)

nums = [5, 2, 8, 1]
nums.sort()
print(nums)   # [1, 2, 5, 8]
nums.sort(reverse=True)
print(nums)   # [8, 5, 2, 1]
  1. A [5, 2, 8, 1]
  2. B [1, 2, 5, 8]
  3. C [8, 5, 2, 1]
  4. D None

Answer: B β€” [1, 2, 5, 8]

.sort() rearranges the list itself into ascending order (in place). For descending: nums.sort(reverse=True).

sort()sorted()

Q11. medium

What is the difference between sort() and sorted()?

nums = [3, 1, 2]
new = sorted(nums)
print(nums)   # [3, 1, 2] - unchanged
print(new)    # [1, 2, 3]
  1. A They are identical
  2. B list.sort() changes the original list; sorted() returns a NEW sorted list and works on any iterable
  3. C sorted() changes the original; sort() returns new
  4. D sorted() only works on numbers

Answer: B β€” list.sort() changes the original list; sorted() returns a NEW sorted list and works on any iterable

nums.sort() modifies nums itself (and returns None). sorted(anything) leaves the original untouched and returns a new sorted list - it even accepts strings, tuples and sets.

sort()sorted()

Q13. medium

What is the output of: matrix = [[1, 2], [3, 4]] print(matrix[1][0])

matrix = [[1, 2],
          [3, 4]]
print(matrix[1][0])   # 3  (row 1, col 0)
  1. A 1
  2. B 2
  3. C 3
  4. D 4

Answer: C β€” 3

Chained indexing: matrix[1] is the second inner list [3, 4], and [0] takes its first element -> 3. Read left to right: row 1, column 0.

Nested lists2D data

Q16. medium

Which of these creates an independent (shallow) COPY of list a?

b = a.copy()   # method
b = a[:]        # full slice
b = list(a)     # constructor - all equivalent
  1. A b = a
  2. B b = a.copy() (or b = a[:])
  3. C b = copy a
  4. D b = a.clone()

Answer: B β€” b = a.copy() (or b = a[:])

a.copy(), a[:] and list(a) all create a new list with the same items. Plain b = a only creates a second name for the same list.

copy()SlicingReferences

Q19. medium

What does [10, 20, 30, 40][1:3] return?

nums = [10, 20, 30, 40]
print(nums[1:3])    # [20, 30]
print(nums[:2])     # [10, 20]
print(nums[::-1])   # [40, 30, 20, 10]
  1. A [20, 30]
  2. B [20, 30, 40]
  3. C [10, 20, 30]
  4. D [30, 40]

Answer: A β€” [20, 30]

List slicing works exactly like string slicing: indexes 1 and 2 (stop 3 excluded) -> [20, 30]. Slices always return a NEW list.

Slicing

Q21. medium

What does nums.index(20) return for nums = [10, 20, 30, 20]?

nums = [10, 20, 30, 20]
print(nums.index(20))    # 1
print(nums.count(20))    # 2 - counts ALL occurrences
  1. A 1 - the position of the FIRST 20
  2. B [1, 3]
  3. C 3
  4. D 20

Answer: A β€” 1 - the position of the FIRST 20

.index(value) returns the position of the first match only: 1. If the value isn't present it raises ValueError - check with in first.

index()count()

Q23. medium

What happens? t = (1, 2, 3) t[0] = 99

t = (1, 2, 3)
# t[0] = 99          # TypeError!
t = (99,) + t[1:]    # build a new tuple
print(t)              # (99, 2, 3)
  1. A t becomes (99, 2, 3)
  2. B TypeError - tuples don't support item assignment
  3. C t becomes (99,)
  4. D Nothing

Answer: B β€” TypeError - tuples don't support item assignment

Tuples are immutable - no assigning, appending or removing after creation. If you need to modify it, convert to a list first or build a new tuple.

ImmutabilityTypeError

Q25. medium

What does tuple unpacking do here? point = (3, 7) x, y = point

point = (3, 7)
x, y = point
print(x, y)   # 3 7
  1. A x = 3 and y = 7
  2. B x = (3, 7) and y is undefined
  3. C Error - tuples can't be split
  4. D x = 7 and y = 3

Answer: A β€” x = 3 and y = 7

Unpacking assigns each tuple element to a variable, position by position: x gets 3, y gets 7. The variable count must match the element count.

UnpackingMultiple assignment

Q26. medium

When should you prefer a TUPLE over a list?

locations = {(0, 0): "origin", (3, 5): "target"}  # tuple keys OK
# {[0, 0]: "x"}   # TypeError - list keys not allowed
  1. A For data that should never change - like coordinates, or dictionary keys
  2. B When you need to add items later
  3. C When sorting frequently
  4. D Never - lists are always better

Answer: A β€” For data that should never change - like coordinates, or dictionary keys

Use tuples for fixed collections: a point (x, y), an RGB color, a date triple. Being immutable makes them safe from accidental changes and usable as dict keys (lists can't be keys).

ImmutabilityDictionary keys

Q28. medium

What is the output of: nums = [1, 2, 3] nums.reverse() print(nums)

nums = [1, 2, 3]
nums.reverse()
print(nums)         # [3, 2, 1]
# nums[::-1] gives a reversed COPY instead
  1. A [3, 2, 1]
  2. B [1, 2, 3]
  3. C None
  4. D Error

Answer: A β€” [3, 2, 1]

.reverse() flips the list in place: [3, 2, 1]. Like sort(), it returns None - the change happens inside the original list.

reverse()reversed()Slicing

Q30. medium

How do you delete the item at index 1 from nums = [10, 20, 30] using del?

nums = [10, 20, 30]
del nums[1]
print(nums)   # [10, 30]
  1. A del nums[1]
  2. B nums.del(1)
  3. C delete nums[1]
  4. D nums - 20

Answer: A β€” del nums[1]

The del statement removes by position: del nums[1] leaves [10, 30]. del can also delete slices (del nums[1:3]) or the whole variable (del nums).

delpop()remove()

Hard questions (5)

Q10. hard

What is the output of: nums = [3, 1, 2] result = nums.sort() print(result)

nums = [3, 1, 2]
print(nums.sort())     # None !
print(nums)             # [1, 2, 3]
print(sorted([3,1,2]))  # [1, 2, 3] - returns a NEW list
  1. A [1, 2, 3]
  2. B None
  3. C [3, 1, 2]
  4. D Error

Answer: B β€” None

Classic trap! .sort() sorts IN PLACE and returns None - the sorted data is in nums, not in result. Use sorted(nums) when you want the sorted list as a return value.

  • Rule: methods that modify in place (sort, append, reverse) return None.

sort() vs sorted()In-place methods

Q15. hard

What is the output? a = [1, 2, 3] b = a b.append(4) print(a)

a = [1, 2, 3]
b = a            # same list, two names!
b.append(4)
print(a)          # [1, 2, 3, 4]

c = a.copy()      # independent copy
c.append(5)
print(a)          # [1, 2, 3, 4] - unaffected
  1. A [1, 2, 3]
  2. B [1, 2, 3, 4]
  3. C [4]
  4. D Error

Answer: B β€” [1, 2, 3, 4]

b = a does NOT copy the list - both names point to the SAME list object. Appending through b changes the one shared list, so a shows [1, 2, 3, 4] too. To get an independent copy: b = a.copy() or b = a[:].

  • One of the most important Python concepts - assignment never copies.

Referencescopy()Aliasing

Q17. hard

What is the difference in result? a = [1, 2]; a.append([3, 4]) versus b = [1, 2]; b.extend([3, 4])

a = [1, 2]
a.append([3, 4])
print(a)   # [1, 2, [3, 4]] - length 3!

b = [1, 2]
b.extend([3, 4])
print(b)   # [1, 2, 3, 4] - length 4
  1. A Both give [1, 2, 3, 4]
  2. B append gives [1, 2, [3, 4]] (nested); extend gives [1, 2, 3, 4] (flattened)
  3. C extend gives [1, 2, [3, 4]]; append flattens
  4. D Both give [1, 2, [3, 4]]

Answer: B β€” append gives [1, 2, [3, 4]] (nested); extend gives [1, 2, 3, 4] (flattened)

append() adds its argument as ONE item - even if it's a list, so you get a list inside a list. extend() unpacks the argument and adds each element individually.

append()extend()+= on lists

Q24. hard

What is the type of x? x = (5)

a = (5)
print(type(a))   # <class 'int'>
b = (5,)
print(type(b))   # <class 'tuple'>
  1. A tuple
  2. B int - parentheses alone don't make a tuple; you need a comma: (5,)
  3. C list
  4. D str

Answer: B β€” int - parentheses alone don't make a tuple; you need a comma: (5,)

(5) is just the number 5 in parentheses - like in math. A one-element tuple needs a trailing COMMA: (5,). The comma makes the tuple, not the parentheses!

  • A famous Python gotcha - remember: the comma makes the tuple.

TuplesCommon gotchas

Q29. hard

What is the output of: print([0] * 3 == [0, 0, 0])

print([0] * 3)              # [0, 0, 0]
grid = [[0] * 3 for _ in range(2)]  # safe 2D grid
  1. A True
  2. B False
  3. C [0, 0, 0]
  4. D Error

Answer: A β€” True

[0] * 3 builds [0, 0, 0], and == on lists compares element by element - same length, same values -> True.

  • Careful: [[0]*3]*2 makes 2 references to the SAME row - use a comprehension for 2D grids.

RepetitionList equality

Practice β€œLists & Tuples” as a quiz