Chapter 12
In This Chapter
Defining why lists are important
Generating lists
Looking through lists
Working with list items sequentially
Changing list content
Locating specific information in lists
Putting list items in order
Using the Counter object to your advantage
A lot of people lose sight of the fact that most programming techniques are based on the real world. Part of the reason is that programmers often use terms that other people don’t to describe these real-world objects. For example, most people would call a place to store something a box or a cupboard — but programmers insist on using the term variable. Lists are different. Everyone makes lists and uses them in various ways to perform an abundance of tasks. In fact, you’re probably surrounded by lists of various sorts where you’re sitting right now as you read this book. So, this chapter is about something you already use quite a lot. The only difference is that you need to think of lists in the same way Python does.
You may read that lists are hard to work with. The reason that some people find working with lists difficult is that they’re not used to actually thinking about the lists they create. When you create a list, you simply write items down in whatever order makes sense to you. Sometimes you rewrite the list when you’re done to put it in a specific order. In other cases, you use your finger as a guide when going down the list to make looking through it easier. The point is that everything you normally do with lists is also doable within Python. The difference is that you must now actually think about what you’re doing in order to make Python understand what you want done.
Lists are incredibly important in Python. This chapter introduces you to the concepts used to create, manage, search, and print lists (among other tasks). When you complete the chapter, you can use lists to make your Python applications more robust, faster, and more flexible. In fact, you’ll wonder how you ever got along without using lists in the past. The important thing to keep in mind is that you have already used lists most of your life. There really isn’t any difference now except that you must now think about the actions that you normally take for granted when managing your own lists.
People create lists to organize information and make it easier to access and change. You use lists in Python for the same reason. In many situations, you really do need some sort of organizational aid to hold data. For example, you might want to create a single place to look for days of the week or months of the year. The names of these items would appear in a list, much as they would if you needed to commit them to paper in the real world. The following sections describe lists and how they work in more detail.
The Python specification defines a list as a kind of sequence. Sequences simply provide some means of allowing multiple data items to exist together in a single storage unit, but as separate entities. Think about one of those large mail holders you see in apartment buildings. A single mail holder contains a number of small mailboxes, each of which can contain mail. Python supports other kinds of sequences as well (Chapter 13 discusses a number of these sequences):
The computer doesn’t view lists in the same way that you do. It doesn’t have an internal notepad and use a pen to write on it. A computer has memory. The computer stores each item in a list in a separate memory location, as shown in Figure 12-2. The memory is contiguous, so as you add new items, they’re added to the next location in memory.
In many respects, the computer uses something like a mailbox to hold your list. The list as a whole is the mail holder. As you add items, the computer places it in the next mailbox within the mail holder.
Depending on what sort of information you place in each mailbox, the mailboxes need not be of the same size. Python lets you store a string in one mailbox, an integer in another, and a floating-point value in another. The computer doesn’t know what kind of information is stored in each mailbox and it doesn’t care. All the computer sees is one long list of numbers that could be anything. Python performs all the work required to treat the data elements according to the right type and to ensure that when you request item five, you actually get item five.
As in real life, before you can do anything with a list, you must create it. As previously stated, Python lists can mix types. However, it’s always a best practice to restrict a list to a single type when you can. The following steps demonstrate how to create Python lists.
You see the familiar Python prompt.
Python creates a list named List1 for you. This list contains two string values (One and Two), an integer value (1), and a Boolean value (True). Of course, you can’t actually see anything because Python processes the command without saying anything.
Notice that each data type that you type is a different color. When you use the default color scheme, Python displays strings in green, numbers in black, and Boolean values in orange. The color of an entry is a cue that tells you whether you have typed the entry correctly, which helps reduce errors when creating a list.
You see the content of the list as a whole, as shown in Figure 12-3. Notice that the string entries appear in single quotes, even though you typed them using double quotes. Strings can appear in either single quotes or double quotes in Python.
Python displays a list of actions that you can perform using lists, as shown in Figure 12-4. Notice that the output is actually a list. So, you’re using a list to determine what you can do with another list.
As you start working with objects of greater complexity, you need to remember that the dir() command always shows what tasks you can perform using that object. The actions that appear without underscores are the main actions that you can perform using a list. These actions are the following:
After you create a list, you want to access the information it contains. An object isn’t particularly useful if you can’t at least access the information it contains. The previous section shows how to use the print() and dir() functions to interact with a list, but there are other ways to perform the task, as described in the following steps.
You see the familiar Python prompt.
Python creates a list named List1 for you.
You see the value 1 as output, as shown in Figure 12-5. The use of a number within a set of square brackets is called an index. Python always uses zero-based indexes, so asking for the element at index 1 means getting the second element in the list.
You see a range of values that includes two elements, as shown in Figure 12-6. When typing a range, the end of the range is always one greater than the number of elements returned. In this case, that means that you get elements 1 and 2, not elements 1 through 3 as you might expect.
You see all the elements, starting from element 1 to the end of the list, as shown in Figure 12-7. A range can have a blank ending number, which simply means to print the rest of the list.
Python displays the elements from 0 through 2. Leaving the start of a range blank means that you want to start with element 0, as shown in Figure 12-8.
To automate the processing of list elements, you need some way to loop through the list. The easiest way to perform this task is to rely on a for statement, as described in the following steps. This example also appears with the downloadable source code as ListLoop.py.
You see an editor in which you can type the example code.
List1 = [0, 1, 2, 3, 4, 5]
for Item in List1:
print(Item)
The example begins by creating a list consisting of numeric values. It then uses a for loop to obtain each element in turn and print it onscreen.
You see a Python Shell window open. The output shows the individual values in the list, one on each line, as shown in Figure 12-9.
You can modify the content of a list as needed. Modifying a list means to change a particular entry, add a new entry, or remove an existing entry. To perform these tasks, you must sometimes read an entry. The concept of modification is found within the acronym CRUD, which stands for Create, Read, Update, and Delete. Here are the list functions associated with CRUD:
The following steps show how to perform modification tasks with lists. This is a hands-on exercise. As the book progresses, you see these same functions used within application code. The purpose of this exercise is to help you gain a feel for how lists work.
You see the familiar Python prompt.
Python creates a list named List1 for you.
Notice that the square brackets are empty. List1 doesn’t contain any entries. You can create empty lists that you fill with information later. In fact, this is precisely how many lists start because you usually don’t know what information they will contain until the user interacts with the list.
The len() function outputs 0, as shown in Figure 12-10. When creating an application, you can check for an empty list using the len() function. If a list is empty, you can’t perform tasks such as removing elements from it because there is nothing to remove.
The len() function now reports a length of 1.
You see the value stored in element 0 of List1, as shown in Figure 12-11.
The insert() function requires two arguments. The first argument is the index of the insertion, which is element 0 in this case. The second argument is the object you want inserted at that point, which is 2 in this case.
Python has added another element to List1. However, using the insert() function lets you add the new element before the first element, as shown in Figure 12-12.
The new list, List2, is a precise copy of List1. Copying is often used to create a temporary version of an existing list so that a user can make temporary modifications to it rather than to the original list. When the user is done, the application can either delete the temporary list or copy it to the original list.
Python copies all the elements in List2 to the end of List1. Extending is commonly used to consolidate two lists.
You see that the copy and extend processes have worked. List1 now contains the values 2, 1, 2, and 1, as shown in Figure 12-13.
Python displays a value of 1, as shown in Figure 12-14. The 1 was stored at the end of the list, and pop() always removes values from the end.
This time, Python removes the item at element 1. Unlike the pop() function, the remove() function doesn’t display the value of the item it removed.
Using clear() means that the list shouldn’t contain any elements now.
You see that the output is 0. List1 is definitely empty. At this point, you’ve tried all the modification methods that Python provides for lists. Work with List1 some more using these various functions until you feel comfortable making changes to the list.
Modifying a list isn’t very easy when you don’t know what the list contains. The ability to search a list is essential if you want to make maintenance tasks easier. The following steps help you create an application that demonstrates the ability to search a list for specific values. This example also appears with the downloadable source code as SearchList.py.
You see an editor in which you can type the example code.
Colors = ["Red", "Orange", "Yellow", "Green", "Blue"]
ColorSelect = ""
while str.upper(ColorSelect) != "QUIT":
ColorSelect = input("Please type a color name: ")
if (Colors.count(ColorSelect) >= 1):
print("The color exists in the list!")
elif (str.upper(ColorSelect) != "QUIT"):
print("The list doesn't contain the color.")
The example begins by creating a list named Colors that contains color names. It also creates a variable named ColorSelect to hold the name of the color that the user wants to find. The application then enters a loop where the user is asked for a color name that is placed in ColorSelect. As long as this variable doesn’t contain the word QUIT, the application continues a loop that requests input.
Whenever the user inputs a color name, the application asks the list to count the number of occurrences of that color. When the value is equal to or greater than one, the list does contain the color and an appropriate message appears onscreen. On the other hand, when the list doesn’t contain the requested color, an alternative message appears onscreen.
Notice how this example uses an elif clause to check whether ColorSelect contains the word QUIT. This technique of including an elif clause ensures that the application doesn’t output a message when the user wants to quit the application. You need to use similar techniques when you create your applications to avoid potential user confusion or even data loss (when the application performs a task the user didn’t actually request).
You see a Python Shell window open. The application asks you to type a color name.
You see a message telling you that the color does exist in the list, as shown in Figure 12-15.
You see a message telling you that the color doesn’t exist, as shown in Figure 12-16.
The application ends. Notice that the application displays neither a success nor a failure message.
The computer can locate information in a list no matter what order it appears in. It’s a fact, though, that longer lists are easier to search when you put them in sorted order. However, the main reason to put a list in sorted order is to make it easier for the human user to actually see the information the list contains. People work better with sorted information.
This example begins with an unsorted list. It then sorts the list and outputs it to the display. The following steps demonstrate how to perform this task. This example also appears with the downloadable source code as SortList.py.
You see an editor in which you can type the example code.
Colors = ["Red", "Orange", "Yellow", "Green", "Blue"]
for Item in Colors:
print(Item, end=" ")
print()
Colors.sort()
for Item in Colors:
print(Item, end=" ")
print()
The example begins by creating an array of colors. The colors are currently in unsorted order. The example then prints the colors in the order in which they appear. Notice the use of the end=" " argument for the print() function to ensure that all color entries remain on one line (making them easier to compare).
Sorting the list is as easy as calling the sort() function. After the example calls the sort() function, it prints the list again so that you can see the result.
You see a Python Shell window open. The application outputs both the unsorted and sorted lists, as shown in Figure 12-17.
Colors = ["Red", "Orange", "Yellow", "Green", "Blue"]
for Item in Colors:
print(Item, end=" ")
print()
Colors.sort()
Colors.reverse()
for Item in Colors:
print(Item, end=" ")
print()
Sometimes you have a data source and you simply need to know how often things happen (such as the appearance of a certain item in the list). When you have a short list, you can simply count the items. However, when you have a really long list, it’s nearly impossible to get an accurate count. For example, consider what it would take if you had a really long novel like War and Peace in a list and wanted to know the frequency of the words the novel used. The task would be impossible without a computer.
You see an editor in which you can type the example code.
from collections import Counter
MyList = [1, 2, 3, 4, 1, 2, 3, 1, 2, 1, 5]
ListCount = Counter(MyList)
print(ListCount)
for ThisItem in ListCount.items():
print("Item: ", ThisItem[0],
" Appears: ", ThisItem[1])
print("The value 1 appears {0} times."
.format(ListCount.get(1)))
In order to use the Counter object, you must import it from collections. Of course, if you work with other collection types in your application, you can import the entire collections module by typing import collections instead.
The example begins by creating a list, MyList, with repetitive numeric elements. You can easily see that some elements appear more than once. The example places the list into a new Counter object, ListCount. You can create Counter objects in all sorts of ways, but this is the most convenient method when working with a list.
The Counter object and the list aren’t actually connected in any way. When the list content changes, you must re-create the Counter object because it won’t automatically see the change. An alternative to re-creating the counter is to call the clear() method first and then call the update() method to fill the Counter object with the new data.
The application prints ListCount in various ways. The first output is the Counter as it appears without any manipulation. The second output prints the individual unique elements in MyList along with the number of times each element appears. To obtain both the element and the number of times it appears, you must use the items() function as shown. Finally, the example demonstrates how to obtain an individual count from the list using the get() function.
A Python Shell window opens, and you see the results of using the Counter object, as shown in Figure 12-18.
Notice that the information is actually stored in the Counter as a key and value pair. Chapter 13 discusses this topic in greater detail. All you really need to know for now is that the element found in MyList becomes a key in ListCount that identifies the unique element name. The value contains the number of times that that element appears within MyList.