Difference between revisions of "List"
(Created page with ''''Lists''' in Python are used to store multiple objects in a single ordered set. Unlike tuples, lists are mutable, so entries can be added, removed, or changed after th…') |
m (→Useful Functions) |
||
Line 22: | Line 22: | ||
==Useful Functions== | ==Useful Functions== | ||
− | For functions that apply to all [[sequence]] types, see [[ | + | For functions that apply to all [[sequence]] types, see [[sequence (Python)#Useful Functions]]. The following are specific to lists. |
*<tt>myList.'''append'''(x)</tt> appends x to the end of myList. | *<tt>myList.'''append'''(x)</tt> appends x to the end of myList. | ||
*<tt>myList.'''insert'''(i, x)</tt> inserts x before the ith element of myList (so x becomes the new ith element, and all subsequent elements are effectively shifted up one index). | *<tt>myList.'''insert'''(i, x)</tt> inserts x before the ith element of myList (so x becomes the new ith element, and all subsequent elements are effectively shifted up one index). |
Revision as of 00:20, 8 April 2011
Lists in Python are used to store multiple objects in a single ordered set. Unlike tuples, lists are mutable, so entries can be added, removed, or changed after the list is created.
The analog to the list in most other programming languages is the array, but lists in Python are more flexible, similar to the Vector class in Java or C++.
Basics
To create a list, place a (possibly empty) comma-separated list of objects or expressions in square brackets. For example, the following initializes a list with three elements:
myList = [1+3, 2.7, 'Thursday']
To access the i'th element of a list, use the list name followed by the index in square brackets.
Note that lists in Python are zero-indexed, so the first element is at index 0, the second is at index 1, and so on. Negative indices wrap around, so -1 is the last element, -2 is the penultimate element, etc. Hence, for the list above, myList[0] will evaluate to 4 while myList[2] and myList[-1] will both evaluate to 'Thursday'.
The elements of lists can be any objects (even functions or other lists!), and they need not all be the same type. The example above, for instance, contains integers, floating point numbers, and strings in the same list.
Changing, Adding, and Removing Elements
Elements of a list can be changed with a simple reassignment. For example, to set the i'th element of myList to x, you would use
myList[i] = x
There are two main ways to add elements to a list. The first is with the append function (see below). The second is using concatenation with + as seen in the following examples:
myList = myList + [x] # appends x to the end of the list (note the brackets around x) myList = [x] + myList # inserts x at the beginning of the list
One way to insert x at an arbitrary position in a list is with the insert function, and you can remove elements from a list with the del function (see below).
Useful Functions
For functions that apply to all sequence types, see sequence (Python)#Useful Functions. The following are specific to lists.
- myList.append(x) appends x to the end of myList.
- myList.insert(i, x) inserts x before the ith element of myList (so x becomes the new ith element, and all subsequent elements are effectively shifted up one index).
- del(myList[i]) removes the ith element of myList (and shifts the indices of subsequent elements down one).
- myList.sort() sorts myList into ascending order. Note that this changes myList, so if you want to preserve the original order you need to copy the list first (myListCopy = myList[:] would work).