Lists and Tuples
Lists and tuples are sequences that can hold objects of any type. Their contents can be of mixed types, so you can have strings, integers, instances, floats, and anything else in the same list. The items in lists and tuples are separated by commas. The items in a list are enclosed in square brackets, and the items in a tuple are enclosed in parentheses. The main difference between lists and tuples is that lists are mutable, and tuples are immutable. This means that you can change the contents of a list, but once a tuple is created, it cannot be changed. If you want to change the contents of a tuple, you need to make a new one based on the content of the current one. Because of the mutability difference, lists have more functionality than tuples—and they also use more memory.
Creating Lists and Tuples
You create a list by using the list constructor, list(), or by just using the square bracket syntax. To create a list with initial values, for example, simply supply the values in brackets:
some_list = [1,2,3] some_list [1, 2, 3]
You can create tuples by using the tuple constructor, tuple(), or using parentheses. If you want to create a tuple with a single item, you must follow that item with a comma, or Python will interpret the parentheses not as indicating a tuple but as indicating a logical grouping. You can also create a tuple without parentheses by just putting a comma after an item. Listing 3.1 provides examples of tuple creation.
Listing 3.1 Creating Tuples
tup = (1,2) tup (1,2) tup = (1,) tup (1,) tup = 1,2, tup (1,2)
You can also use the list or tuple constructors with a sequence as an argument. The following example uses a string and creates a list of the items the string contains:
name = "Ignatius" letters = list(name) letters ['I', 'g', 'n', 'a', 't', 'i', 'u', 's']
Adding and Removing List Items
You can add items to a list and remove items from a list. To conceptualize how it works, think of a list as a stack of books. The most efficient way to add items to a list is to use the append method, which adds an item to the end of the list, much as you could easily add a book to the top of a stack. To add an item to a different position in the list, you can use the insert method, with the index number where you wish to position the new item as an argument. This is less efficient than using the append method as the other items in the list may need to move to make room for the new item; however, this is typically an issue only in very large lists. Listing 3.2 shows examples of appending and inserting.
Listing 3.2 Appending and Inserting List Items
flavours = ['Chocolate', 'Vanilla'] flavours ['Chocolate', 'Vanilla'] flavours.append('SuperFudgeNutPretzelTwist') flavours ['Chocolate', 'Vanilla', 'SuperFudgeNutPretzelTwist'] flavours.insert(0,"sourMash") flavours ['sourMash', 'Chocolate', 'Vanilla', 'SuperFudgeNutPretzelTwist']
To remove an item from a list, you use the pop method. With no argument, this method removes the last item. By using an optional index argument, you can specify a specific item. In either case, the item is removed from the list and returned.
The following example pops the last item off the list and then pops off the item at index 0. You can see that both items are returned when they are popped and that they are then gone from the list:
flavours.pop() 'SuperFudgeNutPretzelTwist' flavours.pop(0) 'sourMash' flavours ['Chocolate', 'Vanilla']
To add the contents of one list to another, you use the extend method:
deserts = ['Cookies', 'Water Melon'] desserts ['Cookies', 'Water Melon'] desserts.extend(flavours) desserts ['Cookies', 'Water Melon', 'Chocolate', 'Vanilla']
This method modifies the first list so that it now has the contents of the second list appended to its contents.
You can assign values to multiple variables from a list or tuple in one line:
a, b, c = (1,3,4) a 1 b 3 c 4
Or, if you want to assign multiple values to one variable while assigning single ones to the others, you can use a * next to the variable that will take multiple values. Then that variable will absorb all the items not assigned to other variables:
*first, middle, last = ['horse', 'carrot', 'swan', 'burrito', 'fly'] first ['horse', 'carrot', 'swan'] last 'fly' middle 'burrito'
For lists you can use built-in sort and reverse methods that can change the order of the contents. Much like the sequence min and max functions, these methods work only if the contents are comparable, as shown in these examples:
name = "Ignatius" letters = list(name) letters ['I', 'g', 'n', 'a', 't', 'i', 'u', 's'] letters.sort() letters ['I', 'a', 'g', 'i', 'n', 's', 't', 'u'] letters.reverse() letters ['u', 't', 's', 'n', 'i', 'g', 'a', 'I']