Page6/20
Lists & Tuples · Page 1 of 2
Lists — Mutable Sequences
Lists & Tuples
Lists — Mutable Sequences
In computer science, a sequence is an ordered collection of elements where each item is accessible by its position (index). Python's list is a mutable, dynamic sequence — it can hold elements of any type mixed together, and it can grow or shrink at runtime. Internally, Python stores a list as an array of pointers to objects, making it flexible but general-purpose.
In practice, a list is Python's most versatile ordered collection. Lists are mutable — you can change, add, or remove elements after creation.
fruits = ["apple", "banana", "cherry"]
# index 0 index 1 index 2
# index -3 index -2 index -1 (negative)
Indexing & Slicing
fruits[0] # "apple" — first element
fruits[-1] # "cherry" — last element
fruits[1:3] # ["banana", "cherry"] — slice
fruits[::-1] # reversed list
Core List Methods
fruits.append("mango") # add to end
fruits.insert(1, "grape") # insert at index
fruits.remove("banana") # remove by value
fruits.pop() # remove & return last
fruits.sort() # sort in-place
len(fruits) # length
Data Science relevance: Lists are the foundation for NumPy arrays and Pandas Series.
main.py
Loading...
OUTPUT
▶Click "Run Code" to execute…