Python Lists
Server Academy Members Only
Sorry, this lesson is only available to Server Academy members. Create a free account now to get instant access to this and more free courses. Click the Sign Up Free button below to get access to our free courses now, or sign in if you have an account.
In Python, a list is a data structure that stores an ordered collection of items. Lists are very flexible and they can store items of any data type, including other lists. In Python, you can access and modify the items in a list using indexing and slicing.
Creating a List
To create a list in Python, use square brackets []
and separate the items in the list with commas. Here are some examples of creating lists in Python:
# Create an empty list
my_list = []
# Create a list of numbers
numbers = [1, 2, 3, 4, 5]
# Create a list of strings
colors = ["red", "green", "blue"]
# Create a list of mixed data types
mixed = [1, "two", 3.0, [4, 5]]
As you can see, a list can store items of any data type, including other lists.
Accessing and Modifying List Items
In Python, you can access the items in a list using indexing. To access an item in a list, use the square bracket notation []
and specify the index of the item you want to access. In Python, the index of the first item in a list is 0, and the index of the last item in a list is -1. Here are some examples of accessing items in a list:
# Access the first item in a list
print(numbers[0]) # Output: 1
# Access the last item in a list
print(numbers[-1]) # Output: 5
# Access a range of items in a list
print(numbers[1:3]) # Output: [2, 3]
list, use the square bracket notation []
and specify the index of the item you want to modify. Then, use the assignment operator =
to assign a new value to that item. Here are some examples of modifying items in a list:
# Modify an item in a list
numbers[0] = 10
print(numbers) # Output: [10, 2, 3, 4, 5]
# Add an item to the end of a list
numbers.append(6)
print(numbers) # Output: [10, 2, 3, 4, 5, 6]
# Remove an item from a list
numbers.remove(3)
print(numbers) # Output: [10, 2, 4, 5, 6]
As you can see, indexing and the assignment operator =
can be used together to modify the items in a list.
Common List Methods
In Python, lists have many built-in methods that allow you to manipulate and transform the items in a list. Here are some common list methods that you might find useful:
append()
: adds an item to the end of a listremove()
: removes an item from a listsort()
: sorts the items in a list in ascending or descending orderreverse()
: reverses the order of the items in a listindex()
: returns the index of an item in a listcount()
: returns the number of times an item appears in a list
Here are some examples of using these list methods:
# Add an item to the end of a list
numbers.append(7)
print(numbers) # Output: [10, 2, 4, 5, 6, 7]
# Remove an item from a list
numbers.remove(5)
print(numbers) # Output: [10, 2, 4, 6, 7]
# Sort the items in a list in ascending order
numbers.sort()
print(numbers) # Output: [2, 4, 6, 7, 10]
# Sort the items in a list in descending order
numbers.sort(reverse=True)
print(numbers) # Output: [10, 7, 6, 4, 2]
# Reverse the order of the items in a list
numbers.reverse()
print(numbers) # Output: [2, 4, 6, 7, 10]
# Get the index of an item in a list
print(numbers.index(6)) # Output: 2
# Count the number of times an item appears in a list
print(numbers.count(4)) # Output: 1
As you can see, the list methods in Python provide a powerful way to manipulate and transform the items in a list.
Conclusion
Overall, lists are an essential data structure in Python that allows you to store and manipulate collections of items. Lists are very flexible and they can store items of any data type, including other lists. You can access and modify the items in a list using indexing and slicing, and you can use list methods to manipulate and transform the items in a list.
Server Academy Members Only
Want to access this lesson? Just sign up for a free Server Academy account and you'll be on your way. Already have an account? Click the Sign Up Free button to get started..
Saving Progress...
Python 3 for Beginners
Installing Python on Windows • 1hr 17min
0 / 4 lessons complete
Section Overview
Free Preview Lesson
Text | 1 min
Downloading and Installing Python on Windows
Free Preview Lesson
Text | 8 min
Installing and configuring VS Code for Python
Free Preview Lesson
Text | 8 min
Lab: Installing Python
Devops Lab | 60
Python Basics • 28min
0 / 7 lessons complete
Section Overview
Text | 2 min
Executing Python Code
Free Preview Lesson
Text | 3 min
Python 3 Syntax
Free Preview Lesson
Text | 5 min
Help! Python Keywords
Free Preview Lesson
Text | 4 min
Printing to the console!
Text | 5 min
Python Operators
Text | 4 min
Section Review
Quiz | 5 min
Python Variables • 41min
0 / 8 lessons complete
Section Overview
Text | 4 min
Casting
Text | 4 min
Strings
Text | 5 min
Booleans
Text | 8 min
User Input
Text | 2 min
Numbers
Text | 7 min
NoneType
Free Preview Lesson
Text | 5 min
Assignment: Write a Mad Libs Script
Text | 6 min
Even more Python Variables! • 41min
0 / 6 lessons complete
Conditional Statements • 15min
0 / 3 lessons complete
Writing Functions • 30min
0 / 5 lessons complete
Section Overview
Text | 3 min
Defining Functions
Text | 4 min
Importing functions from another file
Text | 4 min
Assignment: Rock, paper, scissors
Text | 12 min
Assignment: Count Dictionary Words
Text | 7 min
Python Loops • 23min
0 / 5 lessons complete
Section Overview
Text | 2 min
For In Loops
Text | 5 min
While Loops
Text | 5 min
Nested Loops
Text | 3 min
Python Loops Challenge!
Text | 8 min
Python PIP and Modules • 18min
0 / 4 lessons complete
Section Overview
Text | 3 min
Installing Python PIP
Text | 4 min
Installing Modules with PIP
Text | 5 min
Importing Modules
Text | 6 min
RegEx • 26min
0 / 4 lessons complete
Section Overview
Text | 4 min
Regex 101
Text | 10 min
Importing Regex and manipulating strings
Text | 7 min
Regex Challenge!
Text | 5 min
Working with APIs • 12min
0 / 3 lessons complete
Making HTTP Requests
Text | 3 min
Working with JSON
Text | 5 min
Get your weather with the OpenWeatherMap
Text | 4 min
Course Conclusion • 2min
0 / 1 lessons complete