6/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…