Alright ! Let’s talk about lists in Python they’re like arrays or vectors from other programming languages, but with some cool twists. Creating a list is easy peasy: just put some stuff inside square brackets and voila! Here’s an example:
# Creating a list called "my_list" with three elements: an integer, a string, and a boolean value
my_list = [1, "hello", True]
Now that we have our list, what can we do with it? Well, for starters, you can access individual elements using their index. Just like in math class when your teacher would ask you to find the value of x at position 3 (which was always a trick question because there’s no such thing as x).
Anyway, let’s say we want to get the second element from our list:
# Accessing individual elements in a list using their index
# In this example, we want to get the second element from our list
# Create a list with some elements
my_list = ["hi", "hello", "hey"]
# Use the index 1 to access the second element in the list
second_element = my_list[1]
# Print the second element
print(second_element) # Output: "hello"
But what if you don’t know exactly which index you need? Maybe you just want to loop through the entire list and do something with each element. That’s where a for loop comes in handy! Here’s an example that prints out every item in our list:
# This for loop iterates through each item in the list and prints it out
for item in my_list:
print(item) # This line prints out the current item in the list
Output:
# This script prints "hello" and a boolean value of True
# The first line is unnecessary and can be removed
print("hello") # Prints "hello" to the console
print(True) # Prints a boolean value of True to the console
Now, let’s say you want to add something to your list. You can use the append() method! Here’s an example that adds “world” to our existing list:
# Create a list with different data types
my_list = [1, 'hello', True]
# Use the append() method to add "world" to the end of the list
my_list.append("world")
# Print the updated list
print(my_list) # Output: [1, 'hello', True, 'world']
# The append() method adds a single element to the end of a list
# The updated list now includes the string "world" at the end
And what if you want to remove something from your list? You can use the remove() method! Here’s an example that removes “True” from our existing list:
# This script demonstrates how to remove an item from a list using the remove() method.
# First, we define a list with some values
my_list = [1, 'hello', 'world', True]
# We can use the remove() method to remove an item from the list
my_list.remove(True) # Removes the first occurrence of "True" from the list
print(my_list) # Output: [1, 'hello', 'world']
# The remove() method modifies the original list, so we don't need to assign the result to a new variable.
But what if you want to remove a specific element at a certain index? You can use the pop() method! Here’s an example that removes “hello” from our existing list using its index (which is 1):
# This script demonstrates how to remove a specific element at a certain index from a list using the pop() method.
# Create a list with elements 1, "hello", and "world"
my_list = [1, "hello", "world"]
# Use the pop() method to remove the element at index 1 (which is "hello")
my_list.pop(1)
# Print the updated list
print(my_list) # Output: [1, 'world']
# The pop() method removes and returns the element at the specified index.
# In this case, it removes "hello" and returns it, leaving the list with only [1, 'world'].
# The index of an element in a list starts at 0, so "hello" is at index 1.
# This is why we use my_list.pop(1) to remove it.
And finally, let’s say you want to check if a certain element exists in your list. You can use the index() method! Here’s an example that checks if “hello” is in our existing list:
# Checking if "hello" exists in my_list
if "hello" in my_list: # Checks if "hello" is in my_list using the "in" operator
print("Yay, 'hello' is in there!") # Prints a message if "hello" is found in my_list
else:
print("Oh no, 'hello' isn't in there.") # Prints a message if "hello" is not found in my_list
Now the costly operations. Inserting or deleting an element from the beginning of a list can become expensive because all elements need to be shifted. However, insertion and deletion at the end of the list are less costly as we don’t have to shift any elements. This is similar to Vectors in C++ or ArrayLists in Java.
We hope you found this tutorial helpful!