Lists
Lists are a versatile data type in Python that allows you to create structures to hold and manipulate elements. In Python, lists are incredibly flexible, allowing you to include elements of various data types.
Creating a List
To create a list, use square brackets and include the elements you want inside. Elements can be of any data type, making lists highly adaptable.
# Example of creating a list of strings (names of people)
my_list = ["Bob", "James", "Tom"]
Accessing Elements
Accessing elements in a list is done using indexing, with the index starting at 0.
# Accessing elements by index
first_person = my_list[0] # Retrieves "Bob"
second_person = my_list[1] # Retrieves "James"
Basic Operations on Lists
Append
Add an element to the end of the list.
my_list.append("Jeremy")
# Result: ["Bob", "James", "Tom", "Jeremy"]
Remove
Remove a specific element from the list.
my_list.remove("Bob")
# Result: ["James", "Tom", "Jeremy"]
Pop
Remove and return the last element from the list.
my_list.pop()
# Result: ["James", "Tom"]
Change Element Value
Change the value of an element at a specific index.
my_list[0] = "Charlotte"
# Result: ["Charlotte", "Tom"]
Insert
Insert an element at a specified position.
my_list.insert(1, "Timothy")
# Result: ["Charlotte", "Timothy", "Tom"]
Clear
Remove all elements from the list.
my_list.clear()
# Result: []