Python Programming · Week 5 notes
Lists
Introduction to Lists
What are Lists in Python?
Lists are one of the most versatile and commonly used data structures in Python. They allow you to store multiple items in a single variable.
Key Characteristics of Lists:
- Lists are ordered: The items have a defined order, and that order will not change.
- Lists are mutable: You can change, add, and remove items in a list after it is created.
- Lists allow duplicate values: Since lists are indexed, they can have items with the same value.
- Lists can contain different data types: A single list can contain items of different types (integers, strings, other lists, etc.).
Creating a List
In Python, you create a list by placing all the items (elements) inside square brackets [ ], separated by commas.
# An empty list
empty_list = []
# A list of integers
numbers = [1, 2, 3, 4, 5]
# A list with mixed data types
mixed_list = [1, "Hello", 3.14, True]
# A nested list
nested_list = [1, [2, 3], 4]
Accessing List Elements
You can access list elements by referring to the index number. In Python, indices start at 0.
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Output: apple
print(fruits[1]) # Output: banana
print(fruits[-1]) # Output: cherry (negative indexing starts from the end)
Lists are fundamental to Python programming and are used in various applications. In the following lessons, we'll explore more operations and methods you can use with lists.
List Basics
Common List Operations
Python provides a variety of operations that you can perform on lists. Let's explore some of the most common ones.
1. Adding Elements to a List
You can add elements to a list using several methods:
# Using append() to add an element to the end of the list
fruits = ["apple", "banana"]
fruits.append("cherry")
print(fruits) # Output: ["apple", "banana", "cherry"]
# Using insert() to add an element at a specific position
fruits.insert(1, "orange")
print(fruits) # Output: ["apple", "orange", "banana", "cherry"]
# Using extend() to add multiple elements
more_fruits = ["grape", "kiwi"]
fruits.extend(more_fruits)
print(fruits) # Output: ["apple", "orange", "banana", "cherry", "grape", "kiwi"]
2. Removing Elements from a List
There are also several ways to remove elements from a list:
# Using remove() to remove a specific element
fruits.remove("banana")
print(fruits) # Output: ["apple", "orange", "cherry", "grape", "kiwi"]
# Using pop() to remove and return an element at a specific index
removed_fruit = fruits.pop(1)
print(removed_fruit) # Output: orange
print(fruits) # Output: ["apple", "cherry", "grape", "kiwi"]
# Using del to remove an element or slice
del fruits[0]
print(fruits) # Output: ["cherry", "grape", "kiwi"]
3. Modifying Elements
You can change the value of a specific item by referring to its index number:
fruits[1] = "blueberry"
print(fruits) # Output: ["cherry", "blueberry", "kiwi"]
4. List Slicing
List slicing allows you to access a specific range of elements in a list:
numbers = [0, 1, 2, 3, 4, 5]
print(numbers[2:5]) # Output: [2, 3, 4]
print(numbers[:3]) # Output: [0, 1, 2]
print(numbers[3:]) # Output: [3, 4, 5]
print(numbers[::2]) # Output: [0, 2, 4] (step of 2)
These operations form the foundation of working with lists in Python. In the next lesson, we'll explore more advanced list methods and techniques.
List Methods
Advanced List Methods and Techniques
Python provides a rich set of built-in methods for working with lists. Let's explore some of the most useful ones.
1. Sorting Lists
You can sort lists using the sort() method or the sorted() function:
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
numbers.sort()
print(numbers) # Output: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
fruits = ["banana", "apple", "cherry", "date"]
sorted_fruits = sorted(fruits)
print(sorted_fruits) # Output: ["apple", "banana", "cherry", "date"]
2. Reversing Lists
You can reverse a list using the reverse() method:
numbers = [1, 2, 3, 4, 5]
numbers.reverse()
print(numbers) # Output: [5, 4, 3, 2, 1]
3. Finding Elements
Use the index() method to find the position of an element:
fruits = ["apple", "banana", "cherry"]
print(fruits.index("banana")) # Output: 1
4. Counting Elements
The count() method returns the number of times an element appears in the list:
numbers = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
print(numbers.count(3)) # Output: 3
5. List Comprehensions
List comprehensions provide a concise way to create lists:
squares = [x**2 for x in range(10)]
print(squares) # Output: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
even_numbers = [x for x in range(20) if x % 2 == 0]
print(even_numbers) # Output: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
These advanced techniques will help you manipulate lists more efficiently in your Python programs.