You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Lists and tuples are Python's primary sequence types. A list is a mutable, ordered collection of items. A tuple is similar but immutable — once created, it cannot be changed. Understanding when to use each is a key part of writing effective Python.
# A list of integers
numbers = [1, 2, 3, 4, 5]
# A list of strings
fruits = ["apple", "banana", "cherry"]
# A mixed list
mixed = [42, "hello", 3.14, True, None]
# An empty list
empty = []
# List from range
digits = list(range(10))
print(digits) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
fruits = ["apple", "banana", "cherry", "date", "elderberry"]
print(fruits[0]) # apple — first element
print(fruits[-1]) # elderberry — last element
print(fruits[1:3]) # ['banana', 'cherry'] — slice
print(fruits[:2]) # ['apple', 'banana'] — first two
print(fruits[3:]) # ['date', 'elderberry'] — from index 3 onwards
print(fruits[::2]) # ['apple', 'cherry', 'elderberry'] — every other
Lists are mutable — you can change them after creation:
fruits = ["apple", "banana", "cherry"]
# Change an element
fruits[1] = "blueberry"
print(fruits) # ['apple', 'blueberry', 'cherry']
# Change a slice
fruits[0:2] = ["avocado", "blackberry"]
print(fruits) # ['avocado', 'blackberry', 'cherry']
fruits = ["apple", "banana"]
# append() — add to the end
fruits.append("cherry")
print(fruits) # ['apple', 'banana', 'cherry']
# insert() — add at a specific position
fruits.insert(1, "avocado")
print(fruits) # ['apple', 'avocado', 'banana', 'cherry']
# extend() — add multiple items
fruits.extend(["date", "elderberry"])
print(fruits) # ['apple', 'avocado', 'banana', 'cherry', 'date', 'elderberry']
# + operator — concatenate lists (creates a new list)
more_fruits = fruits + ["fig", "grape"]
print(more_fruits)
fruits = ["apple", "banana", "cherry", "banana", "date"]
# remove() — remove first occurrence of a value
fruits.remove("banana")
print(fruits) # ['apple', 'cherry', 'banana', 'date']
# pop() — remove and return an element by index
last = fruits.pop() # removes last element
print(last) # date
second = fruits.pop(1) # removes element at index 1
print(second) # cherry
print(fruits) # ['apple', 'banana']
# del — delete by index or slice
numbers = [10, 20, 30, 40, 50]
del numbers[2]
print(numbers) # [10, 20, 40, 50]
# clear() — remove all elements
numbers.clear()
print(numbers) # []
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
# sort() — sort in place
numbers.sort()
print(numbers) # [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
# sort descending
numbers.sort(reverse=True)
print(numbers) # [9, 6, 5, 5, 5, 4, 3, 3, 2, 1, 1]
# sorted() — return a new sorted list (original unchanged)
original = [3, 1, 4, 1, 5]
new_sorted = sorted(original)
print(original) # [3, 1, 4, 1, 5] — unchanged
print(new_sorted) # [1, 1, 3, 4, 5]
# reverse() — reverse in place
original.reverse()
print(original) # [5, 1, 4, 1, 3]
# index() — find the index of a value
print(original.index(4)) # 2
# count() — count occurrences
print(original.count(1)) # 2
# len() — number of elements
print(len(original)) # 5
# Membership test
fruits = ["apple", "banana", "cherry"]
print("banana" in fruits) # True
print("grape" not in fruits) # True
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.