Python Concepts/Lists
Objective
edit
|
Lesson
edit
Python ListseditLists are an important data type that allows you to keep a set of iterable item(s). Lists are mutable sequences. This literally means you can have a list of items, like 1, 2, 3 or "h", "e", "l", "l", "o", "!". A list is denoted by square brackets, >>> []
[]
>>> [1]
[1]
>>> [1, 2, 3, 4 ,5]
[1, 2, 3, 4, 5]
>>> ["h", "e", "l", "l" ,"o", "!"]
['h', 'e', 'l', 'l', 'o', '!']
>>> [1, 102, 2.22, "F", 2/2, 1+2j, False]
[1, 102, 2.22, 'F', 1.0, (1+2j), False]
>>> list("hello")
['h', 'e', 'l', 'l', 'o']
>>> a = 'defgh' ; a
'defgh'
>>> b = list(a) ; b
['d', 'e', 'f', 'g', 'h']
>>>
>>> a = b'defgh' ; a
b'defgh'
>>> b = list(a) ; b
[100, 101, 102, 103, 104]
>>>
>>> list(range(7,13))
[7, 8, 9, 10, 11, 12]
>>>
>>> [[1, 2, 3], [2, 1, 3], [True, False, True]]
[[1, 2, 3], [2, 1, 3], [True, False, True]]
>>> [[[[[[1, 2, 3]]]]]]
[[[[[[1, 2, 3]]]]]]
>>> [[[[ 1, 2, [[1, 2, 3]]]]]]
[[[[1, 2, [[1, 2, 3]]]]]]
>>> a = ['a1','a2']
>>> b = ['b1','b2']
>>> c = ['c1','c2']
>>> L = [a,b,c] ; L
[['a1', 'a2'], ['b1', 'b2'], ['c1', 'c2']]
>>> L[0] ; L[1] ; L[2]
['a1', 'a2']
['b1', 'b2']
['c1', 'c2']
>>> L[0][0] ; L[1][1] ; L[2][0]
'a1'
'b2'
'c1'
>>> b = ['b1','b2']
>>> c = ['c1','c2']
>>> d = ['d1','d2']
>>> a = [b,c,d]
>>>
>>> B = ['B1','B2']
>>> C = ['C1','C2']
>>> D = ['D1','D2']
>>> A = [B,C,D]
>>>
>>> array = [a,A]
>>> array
[[['b1', 'b2'], ['c1', 'c2'], ['d1', 'd2']], [['B1', 'B2'], ['C1', 'C2'], ['D1', 'D2']]]
>>>
>>> array[0]
[['b1', 'b2'], ['c1', 'c2'], ['d1', 'd2']]
>>>
>>> array[1]
[['B1', 'B2'], ['C1', 'C2'], ['D1', 'D2']]
>>>
>>> array[1][2]
['D1', 'D2']
>>>
>>> array[1][2][1]
'D2'
>>>
|
List Indexing
edit
Like strings, lists can be indexed. Indexing can be used to get one item out of the list. For example, if you want to get >>> spam = ["h", "e", "l", "l", "o", "!"]
>>> spam[1]
'e'
>>> spam[5]
'!'
>>> spam[3]
'l'
>>> spam[-1]
'!'
>>> spam[-3]
'l'
>>> spam[-2]
'o'
>>> spam[-5]
'e'
>>> spam[1000]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> spam[-10]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
|
List Slicing
edit
Like strings, lists can be sliced. When you slice a list, it will return a new sliced copy of the original list. >>> spam = [1, 2, 3]
>>> spam[0:3]
[1, 2, 3]
>>> spam[0:]
[1, 2, 3]
>>> spam[1:]
[2, 3]
>>> spam[:1]
[1]
>>> spam[:-1]
[1, 2]
>>> spam[:0]
[]
Lists also support extended slicing, where the third parameter acts as the "step". A step of 1 is the default value. >>> bacon = ["h", "e", "l", "l", "o", ",", " ", "w", "o", "r", "l", "d", "!"]
>>> bacon[::-1]
['!', 'd', 'l', 'r', 'o', 'w', ' ', ',', 'o', 'l', 'l', 'e', 'h']
>>> bacon[::1]
['h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!']
>>> bacon[::2]
['h', 'l', 'o', ' ', 'o', 'l', '!']
>>> bacon[::-2]
['!', 'l', 'o', ' ', 'o', 'l', 'h']
>>> bacon[::-5]
['!', 'w', 'l']
>>> bacon[:7:-2]
['!', 'l', 'o']
The power of slicingedit>>> b = [0,1,2,3,4,5,6] ; b
[0, 1, 2, 3, 4, 5, 6]
>>> b[0:0] = [7,8,9] ; b # insert elements at beginning of list.
[7, 8, 9, 0, 1, 2, 3, 4, 5, 6]
>>>
>>> b[0:2] = [] ; b # delete elements at beginning of list.
[9, 0, 1, 2, 3, 4, 5, 6]
>>>
>>> b = [0,1,2,3,4,5,6] ; b
[0, 1, 2, 3, 4, 5, 6]
>>> b += [7,8,9] ; b # add elements at end of list.
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> b[-2:] = [] ; b # delete elements at end of list.
[0, 1, 2, 3, 4, 5, 6, 7]
>>>
>>> b = [0,1,2,3,4,5,6] ; b
[0, 1, 2, 3, 4, 5, 6]
>>> b[2:5] = [7,8,9,10] ; b # modify interior of list
[0, 1, 7, 8, 9, 10, 5, 6]
>>>
>>> # use of brackets '[]':
>>>
>>> b = [[1,2],[3,4],[5,6]] ; b
[[1, 2], [3, 4], [5, 6]]
>>> b[1:2] = [7,8]+[9,10] ; b
[[1, 2], 7, 8, 9, 10, [5, 6]]
>>>
>>> b = [[1,2],[3,4],[5,6]] ; b
[[1, 2], [3, 4], [5, 6]]
>>> b[1:2] = [[7,8]+[9,10]] ; b
[[1, 2], [7, 8, 9, 10], [5, 6]]
>>>
>>> b = [[1,2],[3,4],[5,6]] ; b
[[1, 2], [3, 4], [5, 6]]
>>> b[1:2] = [[7,8],[9,10]] ; b
[[1, 2], [7, 8], [9, 10], [5, 6]]
>>>
|
List manipulation
edit
Adding a new item to a list is pretty basic. The best way to add a new item is by using the built-in list method >>> spam = [1, 2, 3, 4]
>>> spam.append(5)
>>> spam
[1, 2, 3, 4, 5]
>>> spam.append(12)
>>> spam
[1, 2, 3, 4, 5, 12]
>>> spam.append(6)
>>> spam
[1, 2, 3, 4, 5, 12, 6]
also: >>> spam = [1,2,3] ; eggs = [8,7,6] ; spam ; eggs
[1, 2, 3]
[8, 7, 6]
>>> spam += eggs ; spam
[1, 2, 3, 8, 7, 6]
>>>
>>> toast = [1, 2, 3]
>>> toast[0] = 100
>>> toast
[100, 2, 3]
>>> toast[1] = 200
>>> toast
[100, 200, 3]
>>> toast[2] = 300
>>> toast
[100, 200, 300]
>>> juice = ["a", "b", "c"]
>>> del juice[1]
>>> juice
['a', 'c']
>>> del juice[0]
>>> juice
['c']
>>> del juice[0]
>>> juice
[]
>>> dog = ["bark", "talk", "beg"]
>>> del dog[:]
>>> dog
[]
Now this leads to some important questions, primarily how would you copy >>> spam = [1, 2, 3]
>>> eggs = spam
>>> spam
[1, 2, 3]
>>> eggs
[1, 2, 3]
>>> spam[1] = 22
>>> spam
[1, 22, 3]
>>> eggs
[1, 22, 3]
>>> spam = [1, 2, 3]
>>> eggs = spam[:]
>>> spam
[1, 2, 3]
>>> eggs
[1, 2, 3]
>>> spam[1] = 22
>>> spam
[1, 22, 3]
>>> eggs
[1, 2, 3]
This is an important concept to remember early on so you don't run into any problems in the future. While the above example is accurate, it seems to be true only for small lists. NB the following: Deep copyedit>>> b = [ [4, 4, -119, 16], [-1, 1, -1, 1], [0, 14, -17, 0], [1, 1, -13, 1], [2, 4, 8, 16] ] ; b
[[4, 4, -119, 16], [-1, 1, -1, 1], [0, 14, -17, 0], [1, 1, -13, 1], [2, 4, 8, 16]]
>>>
>>> c = b[:] ; c
[[4, 4, -119, 16], [-1, 1, -1, 1], [0, 14, -17, 0], [1, 1, -13, 1], [2, 4, 8, 16]] # a deep copy of c????
>>>
>>> b[0][1] = 23 ; b ; c
[[4, 23, -119, 16], [-1, 1, -1, 1], [0, 14, -17, 0], [1, 1, -13, 1], [2, 4, 8, 16]] # both have changed
[[4, 23, -119, 16], [-1, 1, -1, 1], [0, 14, -17, 0], [1, 1, -13, 1], [2, 4, 8, 16]] # not a deep copy
>>>
>>> c = copy.deepcopy(b) ; b ; c
[[4, 23, -119, 16], [-1, 1, -1, 1], [0, 14, -17, 0], [1, 1, -13, 1], [2, 4, 8, 16]]
[[4, 23, -119, 16], [-1, 1, -1, 1], [0, 14, -17, 0], [1, 1, -13, 1], [2, 4, 8, 16]]
>>>
>>> b[1][3] = 23 ; b ; c
[[4, 23, -119, 16], [-1, 1, -1, 23], [0, 14, -17, 0], [1, 1, -13, 1], [2, 4, 8, 16]] # only b has changed
[[4, 23, -119, 16], [-1, 1, -1, 1], [0, 14, -17, 0], [1, 1, -13, 1], [2, 4, 8, 16]] # a true deep copy
>>>
See the reference: " Shallow and deep copy operations" |
Built-in List Methods
edit
While the following methods have the appearance of functions and it can be tempting to call them "functions," the correct description is "methods."
edit
|
List Usage
edit
Although lists are useful, they are about four to seven times slower than tuples, because lists aren't as static as tuples. Large lists usually have a higher penalty on performance, so try keep them short. As a general rule of thumb, use lists only when you need to dynamically hold information and values. Using Lists as StackseditThe list methods make it easy to use a list as a stack, where the last element added is the first element retrieved (“last-in, first-out”). To add an item to the top of the stack, use >>> stack = [3, 4, 5]
>>> stack.append(6) # push 6 onto stack
>>> stack += [7] # push 7 onto stack. 7 is last in.
>>> stack
[3, 4, 5, 6, 7]
>>> stack.pop()
7 # 7 is first out.
>>> stack
[3, 4, 5, 6]
>>> stack.pop()
6
>>> stack.pop()
5
>>> stack
[3, 4]
>>>
Using Lists as QueueseditWhile lists are not efficient for this purpose, they can be used to illustrate the concept quite well. A queue is a sequence of elements where the first element added is the first element retrieved (“first-in, first-out”). >>> queue = [] # empty queue
>>>
>>> queue.append('John') ; queue # 'John' is first to arrive
['John']
>>> queue += ['Alex'] ; queue # then 'Alex' arrives and goes to end-of-line.
['John', 'Alex']
>>> queue.append('Terry') ; queue # then 'Terry' arrives and goes to end-of-line.
['John', 'Alex', 'Terry']
>>>
>>> nextOut = queue[0] ; del queue[0] ; nextOut ; queue # 'John' was first in and first out.
'John'
['Alex', 'Terry']
>>> nextOut = queue.pop(0) ; nextOut ; queue # 'Alex' was 2nd in and 2nd out.
'Alex'
['Terry']
>>>
>>> queue.append('Graham') ; queue # then 'Graham' arrives and goes to end-of-line.
['Terry', 'Graham']
>>>
List ComprehensionseditList comprehensions provide a concise way to create lists. Common applications are to make new lists where each element satisfies a certain condition applied to each element of a given list.
>>> a = list(range(-5,7)) ; a
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6]
>>>
List all of the negative numbers in list a: >>> b = [x for x in a if x < 0] ; b
[-5, -4, -3, -2, -1]
>>>
The above is equivalent to: >>> b
[]
>>> for x in a :
... if x < 0 : b += [x]
...
>>> b
[-5, -4, -3, -2, -1]
>>>
>>> len([x for x in a if isinstance(x,float)])
0
>>>
>>> [(i,a[i]) for i in range(len(a)) if a[i] % 2]
[(0, -5), (2, -3), (4, -1), (6, 1), (8, 3), (10, 5)]
>>>
|
Assignments
edit
|
Further Reading or Review
edit
|
References
edit4. Python's documentation:
"3.1.3. Lists", "4.6.4. Lists", "More on lists", " Shallow and deep copy operations", "The del statement", "Using Lists as Stacks", "Using Lists as Queues", "List Comprehensions", "Common Sequence Operations", "Mutable Sequence Types", "How are lists implemented?"
5. Python's methods:
6. Python's built-in functions: