IntroductionLists are a big deal. It really can't be overemphasized what a big deal they are. Lists are not only used for iterating through data, but they are also a popular data storage and categorization method used for handling data as a program is running. For anyone who's programmed in another language, lists are often known as arrays.
Lists may be either very simple or very complex, but they all follow the same rules. Lists may contain different types of data, but be careful when working with it. If you create a list that mixes, say, strings and floats, be sure not to try to call a string method on a list item containing a float.
Remember when the string guides said that strings were just a list of characters. Well, now's the time to be glad that you paid attention to strings. You can do many of the same things that you did with strings with lists, and you can do them in the same way. So, some of this is going to feel like a repeat of the string guide, but don't break focus. There are differences, and there are list specific methods, so assuming that strings and lists are the exact same this will get you into trouble.
Creating a ListCreating a list is a bit different than the other variables that you've learned about so far. You can create a list with absolutely no values in it at all. This is useful for situations where you don't exactly know what will be added to the list because the data isn't in the program yet. There will also be plenty of situations where you won't be sure how many entries will be in a list, so again, creating an empty one and adding data later is the right move.
To create a list, give it a name, just like any other variable, but set that name equal to a set of square brackets.
my_list = Square brackets are the indication of a list, so Python will know that even though there isn't any data there, it is still a list, and it will accept data eventually.
If you do know what you want to put into the list, you can do that too. You can add and subtract entries from a list at any time, so it's not permanent, and you don't have to know all of the entries to start with.
linux_distros = ['Debian', 'Ubuntu', 'Fedora', 'CentOS', 'OpenSUSE', 'Arch', 'Gentoo']Since the name of each distribution is a string, they are all placed in quotes. In this case, the difference between single and double quotes is insignificant. Single quotes generally look cleaner in lists. If this was a list of integers or floats, there would be no quotes. Each entry is separated by a comma to ensure that they are read independently.
Navigating ListsNavigating through a list is the same as navigating through a string. Each entry has a value, referred to as an
index, and they count up from zero going down the list.
linux_distros = ['Debian', 'Ubuntu', 'Fedora', 'CentOS', 'OpenSUSE', 'Arch', 'Gentoo'] print(linux_distros)The example above returns, "Debian," because it is the first entry in the list and has an
For another example, try printing out the entry at
linux_distros = ['Debian', 'Ubuntu', 'Fedora', 'CentOS', 'OpenSUSE', 'Arch', 'Gentoo'] print(linux_distros)Did you get "CentOS?" Good.
Lists, like strings, can count backward from zero to access the other end of the data set. Check which entry is at
linux_distros = ['Debian', 'Ubuntu', 'Fedora', 'CentOS', 'OpenSUSE', 'Arch', 'Gentoo'] print(linux_distros[-2])That's be "Arch." If "Debian" is the starting point at zero, "Gentoo is at negative one, and "Arch" is at negative two.
So, it's important to keep in mind that each entry in a list actually has to
indexes, a positive and a negative. There is nothing wrong with choosing one over the other.
linux_distros = ['Debian', 'Ubuntu', 'Fedora', 'CentOS', 'OpenSUSE', 'Arch', 'Gentoo'] print(linux_distros) print(linux_distros[-7])They both print out "Debian." That means that the entry, "Debian," can be found at the
indexof zero and the
indexof negative seven.
Slicing ListsSlicing lists is another area where lists and strings are nearly the same. You can slice out sections of a list much like you can sections of a string. It is done by specifying a start and end point in the same way as well. Just like with strings, you specify the first
indexto include in the slice followed by the
indexto stop before.
linux_distros = ['Debian', 'Ubuntu', 'Fedora', 'CentOS', 'OpenSUSE', 'Arch', 'Gentoo'] debian_distros = linux_distros[:2] print(debian_distros)The main difference in the output is that slicing a string results in another string, while slicing a list results in a new shorter list.
Of course, you can also slice out the middle of the list too.
linux_distros = ['Debian', 'Ubuntu', 'Fedora', 'CentOS', 'OpenSUSE', 'Arch', 'Gentoo'] rh_distros = linux_distros[2:4] print(rh_distros)Going backwards works here as well.
linux_distros = ['Debian', 'Ubuntu', 'Fedora', 'CentOS', 'OpenSUSE', 'Arch', 'Gentoo'] rh_distros = linux_distros[-5:-3] print(rh_distros)Either way that you go about it, slicing out substrings of data from a list is a fairly easy way to retrieve multiple pieces of information from a list and place them into another.
ConclusionSo, from what you've seen, lists and strings are nearly identical. In the next guide, you will explore some of the differences in the form of the methods available to lists. There are powerful tools available for handle the data held in list form that can make manipulating listed data a breeze.
Table of Contents
- Python Introduction and Installation Guide
- Python Files and the Interpreter
- Experimenting With Numbers and Text In Python
- Python Variables
- Working With Number Variables In Python
- Python String Basics
- Advanced Python Strings
- Python Comments
- Python Lists
- Python List Methods
- Python Multidimensional Lists
- Python Tuples
- Python Boolean Operators
- Python If Statements
- Python While Loops
- Python For Loops
- Python Dictionaries
- Python Advanced Dictionaries
- Python Functions