Introduction to Lists in Python
Lists are one of the most versatile and widely used data structures in Python. They allow you to store and manipulate collections of items, making them essential for various programming tasks. In this guide, we will explore the fundamentals of lists in Python, including how to declare, access, modify, and manipulate lists.
Declaring Lists in Python
In Python, you can declare a list by enclosing comma-separated elements within square brackets (). The elements can be of any data type, and a list can contain elements of different types.
Examples of List Declaration
Here are some examples of list declaration in Python:
# Empty list empty_list =  # List with integers numbers = [1, 2, 3, 4, 5] # List with strings fruits = ["apple", "banana", "cherry"] # List with mixed data types mixed_list = [1, "apple", True, 3.14]
Accessing Elements in a List
Indexing in Lists
In Python, you can access individual elements in a list by using their index position. The index starts from 0 for the first element and increments by 1 for each subsequent element. Negative indexing is also supported, where -1 refers to the last element, -2 refers to the second last element, and so on.
Examples of Accessing List Elements
Here are some examples of accessing elements in a list:
numbers = [1, 2, 3, 4, 5] # Accessing the first element first_number = numbers # Output: 1 # Accessing the last element last_number = numbers[-1] # Output: 5 # Accessing a range of elements subset = numbers[1:3] # Output: [2, 3]
Changing List Elements
Lists in Python are mutable, which means you can modify their elements after they are created. You can assign a new value to a specific index to change the element.
Examples of Modifying List Elements
Here are some examples of modifying list elements:
fruits = ["apple", "banana", "cherry"] # Modifying the second element fruits = "orange" # fruits is now ["apple", "orange", "cherry"] # Modifying a range of elements fruits[1:3] = ["grape", "melon"] # fruits is now ["apple", "grape", "melon"]
Adding Elements to a List
Python provides several methods to add elements to a list. The most commonly used methods are
append() method adds a single element to the end of the list.
fruits = ["apple", "banana"] fruits.append("cherry") # fruits is now ["apple", "banana", "cherry"]
extend() method allows you to add multiple elements to the end of the list by providing an iterable (e.g., another list).
fruits = ["apple", "banana"] fruits.extend(["cherry", "grape"]) # fruits is now ["apple", "banana", "cherry", "grape"]
Removing Elements from a List
Python provides various methods to remove elements from a list. The commonly used methods are
pop() method removes and returns the element at a specific index. If no index is provided, it removes and returns the last element.
Hi all, my name is Angelika and I am one of the authors of the EasyTechh website. Like the rest of our team I am incredibly ambitious and I love helping people.
That’s why I write here and not only here 😉 I write interesting and useful for people articles in the IT sphere and a little bit about life.