Lists

Carson West

Python 1 Home

Lists Lists are ordered, mutable (changeable) sequences of items. They can contain items of different data types.

Creating Lists:

my_list = 1, 2, "hello", 3.14, True]]
empty_list = 1

Accessing Elements: Lists are zero-indexed.

first_element = my_list[0]]  # 1
last_element = my_list[-1 # True

Slicing:

sub_list = my_list1:4  # 2, "hello", 3.14 (exclusive of upper bound)

Methods:

my_list.append(5)
my_list.insert(2, "world")
my_list.remove("hello")
print(my_list)  # Output will depend on previous operations.

List Comprehensions: List Comprehension

Iterating through Lists:

for item in my_list:
    print(item)

for i, item in enumerate(my_list):
    print(f"Item at index {i}: {item}")

List Operations:

Nested Lists: Multidimensional Lists

Mutable vs. Immutable: Mutable vs Immutable Types