Table of Contents
Lists in Python are collections that are both ordered and changeable. They are very similar to what other programming languages call arrays. Lists can contain as many items as you want and of any type.
Creating a List
Let's create our first list:
PYTHONanimals = ["elephant", "tiger", "lion"]
print(animals)
BASH['elephant', 'tiger', 'lion']
The syntax is as straightforward as possible. Values are separated by commas, and they start and end with a bracket.
Accessing Items
The syntax for accessing an item is also pretty standard, simply provide the index of the item you want to access. Note that the first item has an index of 0
:
PYTHONanimals = ["elephant", "tiger", "lion"]
print(animals[0])
print(animals[1])
print(animals[2])
BASHelephant
tiger
lion
You can even use negative values to access it from the end:
PYTHONanimals = ["elephant", "tiger", "lion"]
print(animals[-1])
BASHlion
Changing Items
Instead of accessing the items, you can choose to update the value at that index, like this:
PYTHONanimals = ["elephant", "tiger", "lion"]
print(animals)
animals[1] = "bear"
print(animals)
BASH['elephant', 'tiger', 'lion']
['elephant', 'bear', 'lion']
Appending Items
There is a built-in function to append items to the end of the list:
PYTHONanimals = ["elephant", "tiger", "lion"]
print(animals)
animals.append("bear")
print(animals)
BASH['elephant', 'tiger', 'lion']
['elephant', 'tiger', 'lion', 'bear']
Deleting Items
If there's an item in your list that you want to remove, you can delete it using the del
function and passing in the index.
PYTHONanimals = ["elephant", "tiger", "lion"]
print(animals)
del(animals[1])
print(animals)
BASH['elephant', 'tiger', 'lion']
['elephant', 'lion']
Alternatively, you can use the remove
function which also removes the item given a value:
PYTHONanimals = ["elephant", "tiger", "lion"]
print(animals)
animals.remove("tiger")
print(animals)
BASH['elephant', 'tiger', 'lion']
['elephant', 'lion']
Looping through a List
Looping through a list is a very common operation and Python luckily makes this easy to do:
PYTHONanimals = ["elephant", "tiger", "lion"]
for animal in animals:
print(animal)
BASHelephant
tiger
lion
Checking if an Item Exists in List
Sometimes you might be working with huge lists or not even know what is inside the list, in the case of lists being returned from methods. You can very easily check if a list contains a value you want, like this:
PYTHONanimals = ["elephant", "tiger", "lion"]
print("tiger" in animals)
print("bird" in animals)
BASHTrue
False
List Length
For many different reasons, you'll want to know the number of items inside a list. You can get this value using the len()
function:
PYTHONanimals = ["elephant", "tiger", "lion"]
print(len(animals))
BASH3
Combine Multiple Lists
Python has a simple syntax for combining lists, use the +
.
PYTHONna = ["USA", "Canada", "Mexico"]
eu = ["UK", "France", "Germany"]
countries = na + eu
print(countries)
BASH['USA', 'Canada', 'Mexico', 'UK', 'France', 'Germany']
Reverse a List
Reverse the order of the items in a list using the reverse
function:
PYTHONna = ["USA", "Canada", "Mexico"]
na.reverse()
print(na)
BASH['Mexico', 'Canada', 'USA']
Sort a list
You can easily sort a list using the built-in sort
function:
PYTHONna = ["USA", "Canada", "Mexico"]
na.sort()
print(na)
BASH['Canada', 'Mexico', 'USA']
- Getting Started with TypeScript
- Managing PHP Dependencies with Composer
- How to Serve Static Files with Nginx and Docker
- How to Set Up Cron Jobs in Linux
- Getting Started with Deno
- How to deploy a Node app using Docker
- Getting Started with Sass
- Using Puppeteer and Jest for End-to-End Testing
- Getting Started with Moment.js
- Building a Real-Time Note-Taking App with Vue and Firebase
- Getting Started with React
- Using Axios to Pull Data from a REST API