🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

Lists

AI & DATA SCIENCE // lists

A list is Python's built-in, mutable, ordered sequence type that can hold a mix of any objects and grow or shrink after creation.

Syntax

my_list = [1, 2, 3]
my_list.append(4)
my_list[0]

Deep Dive Course

Lists are one of Python's most-used data structures: ordered, indexable from both ends using positive and negative indices, allow duplicate values, and support in-place mutation through methods like append(), extend(), insert(), remove(), and slicing assignment. Internally, CPython implements a list as a dynamic array of object pointers, so indexing is O(1) but inserting or removing from the front is O(n) since every following element has to shift.

1Understanding Lists

Lists are one of Python's most-used data structures: ordered, indexable from both ends using positive and negative indices, allow duplicate values, and support in-place mutation through methods like append(), extend(), insert(), remove(), and slicing assignment. Internally, CPython implements a list as a dynamic array of object pointers, so indexing is O(1) but inserting or removing from the front is O(n) since every following element has to shift.

💡

Appending to the end of a list is amortized O(1), but inserting at the front is O(n) — use collections.deque instead if you need fast operations at both ends.

editor.html
fruits = ["apple", "banana"]
fruits.append("cherry")
print(fruits)
print(fruits[-1])
localhost:3000

2Practical Example

Here is a real-world application of Lists showing how it is used in production Python code.

editor.html
matrix = [[1, 2], [3, 4]]
alias = matrix
copy = matrix.copy()
alias[0][0] = 99
print(matrix)
print(copy)
localhost:3000

3Best Practices

Follow these guidelines when working with Lists:

1. Use a list comprehension instead of a for loop with .append() when building a new list from a transformation

2. Use collections.deque instead of list when you need fast insertion/removal from both ends

3. Be careful with new_list = old_list — it creates an alias, not a copy; use old_list.copy() or a slice to actually duplicate it

⚠️

Tip: Appending to the end of a list is amortized O(1), but inserting at the front is O(n) — use collections.deque instead if you need fast operations at both ends.

editor.html
fruits = ["apple", "banana"]
fruits.append("cherry")
print(fruits)
print(fruits[-1])
localhost:3000

Examples

Example 01Basic Usage
fruits = ["apple", "banana"]
fruits.append("cherry")
print(fruits)
print(fruits[-1])
Example 02Advanced Example
matrix = [[1, 2], [3, 4]]
alias = matrix
copy = matrix.copy()
alias[0][0] = 99
print(matrix)
print(copy)

Best Practices

  • Use a list comprehension instead of a for loop with .append() when building a new list from a transformation
  • Use collections.deque instead of list when you need fast insertion/removal from both ends
  • Be careful with new_list = old_list — it creates an alias, not a copy; use old_list.copy() or a slice to actually duplicate it

Interview Question

Why does mutating a nested list still affect a copy made with list.copy()?

Hint: Think about shallow vs. deep copies.

list.copy() performs a shallow copy: it creates a new outer list, but the elements inside it are the same object references as the original — so if those elements are themselves mutable, like nested lists, mutating one through the copy still affects the shared inner object. To fully decouple nested structures, use copy.deepcopy() instead.

Exercises

MediumPractice using Lists in a real scenario.
View Solution
fruits = ["apple", "banana"]
fruits.append("cherry")
print(fruits)
print(fruits[-1])

Frequently Asked Questions

Why does mutating a nested list still affect a copy made with list.copy()?

list.copy() performs a shallow copy: it creates a new outer list, but the elements inside it are the same object references as the original — so if those elements are themselves mutable, like nested lists, mutating one through the copy still affects the shared inner object. To fully decouple nested structures, use copy.deepcopy() instead.

Related Functions

tupleslist comprehensionscollections.deque