Python Lists

Introduction

Lists 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 List

Creating 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 Lists

Navigating 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[0])

The example above returns, “Debian,” because it is the first entry in the list and has an index of zero.

For another example, try printing out the entry at index three.

linux_distros = ['Debian', 'Ubuntu', 'Fedora', 'CentOS', 'OpenSUSE', 'Arch', 'Gentoo']
print(linux_distros[3])

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 index negative two.

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[0])
print(linux_distros[-7])

They both print out “Debian.” That means that the entry, “Debian,” can be found at the index of zero and the index of negative seven.

Slicing Lists

Slicing 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 index to include in the slice followed by the index to 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.

Conclusion

So, 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

  1. Python Introduction and Installation Guide
  2. Python Files and the Interpreter
  3. Experimenting With Numbers and Text In Python
  4. Python Variables
  5. Working With Number Variables In Python
  6. Python String Basics
  7. Advanced Python Strings
  8. Python Comments
  9. Python Lists
  10. Python List Methods
  11. Python Multidimensional Lists
  12. Python Tuples
  13. Python Boolean Operators
  14. Python If Statements
  15. Python While Loops
  16. Python For Loops
  17. Python Dictionaries
  18. Python Advanced Dictionaries
  19. Python Functions


Comments and Discussions
Linux Forum