What is a Python List?
Python lists are built-in data types that are used to store multiple items in a single variable.
Creating a Python List
The first step involves selecting the name or variable which is then equated to square brackets. Before we add anything between the brackets this is known as a blank list and it could be useful depending on what you’re doing with your code.
In this example, we are going to create a list called “bears” and we will populate the list manually with string or text. However, a list can be of anything like numbers, text, dictionaries or even other Python lists.
The list will contain the following [‘Polar’, ‘Brown’, ‘Panda’, ‘Grizzly’], we have now constructed a list and we can proceed to print out the list as shown below.
And then inside the parentheses, we need to type the element that we want to add to the list, in this case, ‘Koala’. This is going to add the element to the end of the list. If we now print out the list ‘bears’ again we’ll see it’s added to the end of the list. Adding elements to a Python list
Now to do that we use the append function. So to add an item to our Python list, we simply type the name of the list that we want to add something to and we do dot append. This is going to add something to the end of our list. Now it is important to know that it goes to the end of the list because the list is actually an ordered data type.
Note that If we put the print statement before we do the append function we will obtain the first list that does not contain ‘Koala’. As shown in the code below.
Removing Item(s) from a Python List
Now to remove an item from a list we simply want to use pop. Now pop does what it actually sounds like which is popping the item out of the list. We also create a variable to save the popped item.
This can be quite useful and is definitely worth knowing. Let’s proceed to create a variable that goes by the name “item_popped” and then equate it to “bears.pop” with parentheses at the end. Now to remove an item from the list all that we ought to do is give the index number of the item in the list that we want to remove.
Remember that python is a Zero index language therefore it means that items in the Python list start from index zero.
Note that when we print the list out again afterwards we can see that the item ‘Polar’ will now have been removed but we still have it stored in the variable ‘item_popped’.
Slicing and Indexing a Python List
Now to index an item in a list we will say the item is equal to bears and we’re going to do the square brackets to access each part of it with our index. As we just mentioned python is a zero index language. So if I do zero and then I print out the item in index zero, this is going to now be the first item in this list.
Now you can actually use negative numbers as well. For instance, a minus one is an item at the end of the list. So the first one is a zero and then minus one will go to the actual end.
This is really handy if you wanted to access an item at the end of the list without working out how long it is and then doing that number, this will give us Koala as demonstrated in the code below.
To access multiple parts of the data we can use a slicer. This is going to separate two numbers with a colon and it’s going to get the information in between those two indexes.
For example, if we place [0:2] we are going to get the first and second items in the list because it is up to but not including 2. The output of this code is a new list with those two items in it.
Now we can actually just remove the zero because then the code will then just interpret that as the beginning of the Python list. So we’ll get the same results. Furthermore, we can actually do this the other way around as well and we can say it will have everything from the second, all the way to the end of the list. So that’s going to give us the last three items from the list as the output.
Summary
This is how Python lists work in practice. If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.
In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
Programming tutorials
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to use the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.
Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING
Not sure if programming is for you? With CodeBerry you’ll like it.
