Python - Lists
Introduction
I’ll assume that you’ve read last post, so one more topic you’ll need to understand before you can begin writing programs in earnest is the list data type and it’s cousin, the tuple. They can contain multiple values, which makes it easier to write programs that handle large amounts of data. And since lists themselves can contain other lists, you can use them to arrange data into hierarchical structures.
The List Data Type
A list is a value that contains multiple values in an ordered sequence. A list value looks like this : [‘Russia’,’Moscow’,’Samara’]. Just as string values are typed with quote characters to mark where the string begins and ends, a list begins with an opening square bracket and ends with a closing square bracket, []. Values inside the list are also called items. Items are seperated with commas.
Python lets you index the list values, it start with a 0. If you use an index that exceeds the number of values in your list value, it will an IndexError : List index out of range. Note that you can use negative indexes. The integer value -1 refers to the last index in a list, the value -2 refers to the second-of-last index in a list, and so on.
Getting Sublists with Slices : Just as an index can get a single value from a list, a slice can get several values from a list, in the form of a new list. A slice is typed between square brackets, like an index, but it has two integers seperated by a colon. In a slice, the first integer is the index where the slice starts. The second integer is the index where the slice ends. A slice hoes up to, but will not include, the value at the second index. A slice evaluates to a new list value. Here’s an example of what I’ve been talking about.
As a shortcut, you can leave out one or both of the indexes on either side of the colon in the slice. Leaving out the first index is the same as using 0 and leaving the second is the same as using the length of the list.
Note that you can concatenate, replicate, change the values of a list. For removing values from lists, we can use the del statement.
The “in” and “not in” Operators
You can determine whether a value is or isn’t in a list with in and not in operators. Like other operators, in and not in are used in expressions and connect two values: a value to look for in a list and the list where it may be found. These expressions will evaluate to a boolean value.
Assignement
In lists, we can use th multiple assignement and the augmented assignement operators. The example below describes it all.
Methods
A method is the same as a function, except it is “called on” a value. Each data type has its own set of methods. The list data type, for example, has several useful methods for finding, adding, removing, and otherwise manipulating values in a list.
Finding a Value in a list with index() Method
List values have an index() method that can be passed a value, and if that value exists in the list, the index of the value is returned. If the value isn’t in the list, the Python produces a Value Error.
When there are duplictes of the value in the list, the index of it’s first appearence is returned.
Adding Values to Lists with the append() and insert() Methods
The append() method call adds the argumennt to the end of the list. The insert() method can insert a value at any index in the list. The first argument to insert() is the index for the new value, and the second argument is the new value to be inserted. As I said before, methods belong to a single data type. The append() and insert() methods are list methods and can be called only on list values, not on other values such as strings or integers.
Removing Values from lists with remove()
The remove() method is passed the value to be removed from the list it is called on. If the value appears multiple times in the list, only the first instance of the value will be removed.
The del statement is good to use when you know the index of the value you want to remove from the list. The remove() method is good when you know to value you want to remove from the list.
Sorting the Values in a list with the sort() method
Lists of number values or lists of strings can be stored with the sort method. You can also pass True for the reverse keyword argument to have sort() sort the values in reverse order. There are three things you should note about the sort() method:
- First, the sort() method sorts the list in place; don’t try to capture the return value.
- You cannot sort lists that have both number values and string values in them, since Python doesn’t know how to compate values.
- sort() uses “ASCIIbetical oreder” rather than actual alphabetical order for sorting strings. This means uppercase letters come before lowercase letters.
List-like Types : Strings and Tuples
Lists aren’t the only data types that represent ordered sequences of values. For example, string and lists are actually similar, if you consider a string to be a “list” of single text characters. But lists and strings are different in an important way. A list value is a mutable data type: It can values added, removed or changed. However, a string is immutable: It cannot be changed. Trying to reassign a single character in a string results in a TypeError. The tuple data type is almost identical to the list data type, exepct in two ways :
- Tuples are typed with parentheses.
- Tuples are immutable, tuples cannot have their values modified, appended, or removed.
If you have only one value in your tuple, you can indicate this by placing a trailing camma after the value inside the parentheses. Otherwise python will think you’ve just typed a value inside regular parentheses.
You can easily converts list to tuple or a tuple to a list using list() and tuple() functions.
References
Lists don’t get assigned the same way as other types. When you assign a list to a variable, you are actually assiging a list reference to the variable. A reference is a value that points to some bit of data, and a list reference is a value that points to a list. Although passing around references is often the handiest way to deal with lists, if the function modifies the list that is passed, you may not want these changes in the original list. For this Python provides a module named copy that provides both the copy() and deepcopy() functions. The first one can be used to make a duplicate copy of a mutable value like a list or dictionary, not just a copy of a reference. If the list you need to copy contains lists, then use the deepcopy() function which will copy the inner lists as well.
Enough for today, see you tomorrow <3