Definition
📌 Lists
The list is Python's most-used data structure — an ordered, mutable (changeable) collection that can hold items of any type, and grow or shrink as needed.
Syntax
syntax
myList = [item1, item2, item3]
myList.append(item)
myList.remove(item)
myList[index]
myList[start:end]Example
py
fruits = ["apple", "banana", "cherry"]
fruits.append("mango")
fruits.remove("banana")
print(fruits)
print("First:", fruits[0])
print("Slice:", fruits[1:3])Output
output['apple', 'cherry', 'mango']
First: apple
Slice: ['cherry', 'mango']
Important Points
- Lists are ordered and mutable — items can be added, removed, or changed after creation.
- Common methods:
.append(),.remove(),.sort(),.pop(),.insert(). - List comprehensions (
[x*2 for x in numbers]) are a concise, Pythonic way to build new lists. - Lists can be nested — a list of lists — to represent grids or tables.
Common Mistakes
- Trying to remove an item that doesn't exist with
.remove(), which raises aValueError. - Confusing
.append()(adds one item) with.extend()(adds multiple items from another iterable). - Modifying a list while iterating over it directly with a
forloop, which can skip elements unexpectedly.
Interview Tip
🎯 Before your interview
List comprehensions are a favorite interview topic — be ready to rewrite a simple for-loop that builds a list as a one-line comprehension.
