DevAcademia
C++C#CPythonJava
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz

Loading Python tutorial…

Loading content
Python FundamentalsTopic 24 of 77
←PreviousPrevNextNext→

Python Lists

List Fundamentals

Lists are ordered, mutable sequences and one of the most commonly used data structures in Python. They can hold elements of any type and adjust size dynamically.

Key characteristics:

- Ordered (maintain insertion order)

- Mutable (elements can be updated or removed)

- Heterogeneous (can store mixed data types)

- Indexable and sliceable

- Dynamically resizable

Example
# List creation examples
primes = [2, 3, 5, 7, 11]
mixed = [1, 'two', 3.0, True]
matrix = [[1, 2], [3, 4]]

# Using list() constructor
chars = list('hello')  # ['h', 'e', 'l', 'l', 'o']
Output
# Results shown in comments

Memory Management

Lists are backed by dynamic arrays that grow with overallocation to reduce frequent resizing:

- Begin with small capacity and expand as needed

- Growth pattern roughly follows 0, 4, 8, 16, 25, 35, ...

- `append()` runs in amortized O(1) time

- Arbitrary insertions require shifting elements, costing O(n)

Example
import sys
lst = []
for i in range(10):
    print(f"Length: {len(lst)}, Size in bytes: {sys.getsizeof(lst)}")
    lst.append(i)
ℹ️ Note: Over-allocation ensures efficient appends by minimizing costly reallocations.

Performance Characteristics

OperationTime ComplexityExample
Index accessO(1)lst[5]
AppendO(1) amortizedlst.append(x)
InsertO(n)lst.insert(0, x)
Pop lastO(1)lst.pop()
Pop at indexO(n)lst.pop(2)
Search (in)O(n)x in lst
SliceO(k)lst[2:5]
SortO(n log n)lst.sort()
Test your knowledge: Python Lists
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Python FundamentalsTopic 24 of 77
←PreviousPrevNextNext→