In Lesson 1 you learned about some common data types in Python, such as strings and integers. Sometimes you need a type that can store multiple related values together. Python offers several ways of doing this, and the first one we'll learn about is the list.
Here's a simple example of a list. You can type this in the PythonWin Interactive Window to follow along:
>>>suits = ['Spades', 'Clubs', 'Diamonds', 'Hearts']
This list named 'suits' stores four related string values representing the suits in a deck of cards. In many programming languages, storing a group of objects in sequence like this is done with arrays. While the Python list could be thought of as an array, it's a little more flexible than the typical array in other programming languages. This is because you're allowed to put multiple data types into one list.
For example, suppose we wanted to make a list for the card values you could draw. The list might look like this:
>>>values = ['Ace', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King']
Notice that you just mixed string and integer values in the list. Python doesn't care. However, each item in the list still has an index, meaning an integer that denotes each item's place in the list. The list starts with index 0 and for each item in the list, the index increments by one. Try this:
>>>print suits Spades >>>print values King
In the above lines, you just requested the item with index 0 in the suits list and got 'Spades'. Similarly, you requested the item with index 12 in the values list and got 'King'.
It may take some practice initially to remember that your lists start with a 0 index. Testing your scripts can help you avoid off-by-one errors that might result from forgetting that lists are zero-indexed. For example, you might set up a script to draw 100 random cards and print the values. If none of them is an Ace, you've probably stacked the deck against yourself by making the indices begin at 1.
Remember you learned that everything is an object in Python? That applies to lists too. In fact, lists have a lot of useful methods that you can use to change the order of the items, insert items, sort the list, and so on. Try this:
>>> suits = ['Spades', 'Clubs', 'Diamonds', 'Hearts'] >>> suits.sort() >>> print suits ['Clubs', 'Diamonds', 'Hearts', 'Spades']
Notice that the items in the list are now in alphabetical order. You may have also noticed that when you typed "suits" you got some help to see what methods were available for the list. This is called autocompletion, and it can be a great help when you're writing code. You want to understand what you can do and cannot do. It can also be a way of avoiding typing errors because you can arrow down and press Tab to insert the method you want. The autocompletion is a feature of PythonWin, but this type of help can be found in other integrated development environments (IDEs) like Microsoft Visual Studio. Microsoft has branded their version of autocompletion, "IntelliSense," and this name has been catchy enough that you may hear people using it conversationally even when using non-Microsoft IDEs.
The sort() method you used above allowed you to do something in one line of code that would have otherwise taken many lines. Another helpful method like this is reverse(), which allows you to sort a list in reverse alphabetical order:
>>> suits.reverse() >>> print suits ['Spades', 'Hearts', 'Diamonds', 'Clubs']
Before you attempt to write list-manipulation code, check your textbook or the Python list reference documentation to see if there's an existing method that might simplify your work.
Inserting items and combining lists
What happens when you want to combine two lists? Type this in the Interactive Window:
>>> listOne = [101,102,103] >>> listTwo = [104,105,106] >>> listThree = listOne + listTwo >>> print listThree [101, 102, 103, 104, 105, 106]
Notice that you did not get [205,207,209]; rather, Python treats the addition as appending listTwo to listOne. Next, try these other ways of adding items to the list:
>>> listThree +=  >>> print listThree [101, 102, 103, 104, 105, 106, 107] >>> listThree.append(108) >>> print listThree [101, 102, 103, 104, 105, 106, 107, 108]
To put an item at the end of the list, you can either add a one-item list (how we added 107 to the list) or use the append() method on the list (how we added 108 to the list). Notice that listThree +=  is a shortened form of saying listThree = listThree + .
If you need to insert some items in the middle of the list, you can use the insert() method:
>>> listThree.insert(4, 999) >>> print listThree [101, 102, 103, 104, 999, 105, 106, 107, 108]
Notice that the insert() method above took two parameters. You might have even noticed a tooltip that shows you what the parameters mean.
The first parameter is the index position that the new item will take. This method call inserts 999 between 104 and 105. Now 999 is at index 4.
Getting the length of a list
Sometimes you'll need to find out how many items are in a list, particularly when looping. Here's how you can get the length of a list:
>>> myList = [4,9,12,3,56,133,27,3] >>> print len(myList) 8
Notice that len() gives you the exact number of items in the list. To get the index of the final item, you would need to use len(myList) - 1. Again, this distinction can lead to off-by-one errors if you're not careful.
Other ways to store collections of data
Lists are not the only way to store ordered collections of items in Python; you can also use tuples and dictionaries. Tuples are like lists, but you can't change the objects inside a tuple over time. In some cases a tuple might actually be a better structure for storing values like the suits in a deck of cards, because this is a fixed list that you wouldn't want your program to change by accident.
Dictionaries differ from lists in that items are not indexed; instead, each item is stored with a key value which can be used to retrieve the item. We'll use dictionaries later in the course, and your reading assignment for this lesson covers dictionary basics. The best way to understand how dictionaries work is to play with some of the textbook examples in the Interactive Window (see Zandbergen 6.8).