|
|
Line 1: |
Line 1: |
− | '''Lists''' in [[Python]] are used to store multiple objects in a single ordered set. Unlike [[tuple]]s, 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 [[integer]]s, [[floating point number]]s, and [[string]]s 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.
| |
− | *<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>'''del'''(myList[i])</tt> removes the ith element of myList (and shifts the indices of subsequent elements down one).
| |
− | *<tt>myList.'''sort'''()</tt> 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 (<tt>myListCopy = myList[:]</tt> would work).
| |
− |
| |
− | ==See Also==
| |
− | [http://docs.python.org/py3k/library/stdtypes.html#mutable-sequence-types Python 3.2 Documentation]
| |
− |
| |
− | [[Category:Introduction to Programming]]
| |