chapter 2 Lists Flashcards
If a function call returns a sequence, you can index it directly.
> > > fourth = input(‘Year: ‘)[3]
Year: 2005
fourth
‘5’
When slicing, you specify (either explicitly or implicitly) the start and end points of the slice. Another parameter,
which normally is left implicit
> > > numbers[0:10:1]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
if you want every fourth element of a sequence, you need to supply only a step size of four.
> > > numbers[::4]
[1, 5, 9]
Naturally, the step size can’t be zero—that wouldn’t get you anywhere—but it can be negative, which means
extracting the elements from right to left.
>>> numbers[8:3:-1] [9, 8, 7, 6, 5] >>> numbers[10:0:-2] [10, 8, 6, 4, 2] >>> numbers[0:10:-2] [] >>> numbers[::-2] [10, 8, 6, 4, 2] >>> numbers[5::-2] [6, 4, 2] >>> numbers[:5:-2] [10, 8]
Sequences can be concatenated with the addition (plus) operator. you cannot concatenate sequences of different types.
>>> [1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] >>> 'Hello,' + 'world!' 'Hello, world!' >>> [1, 2, 3] + 'world!' Traceback (innermost last): File "", line 1, in ? [1, 2, 3] + 'world!' TypeError: can only concatenate list (not "string") to list
Multiplying a sequence by a number x creates a new sequence where the original sequence is repeated x times:
> > > ‘python’ * 5
‘pythonpythonpythonpythonpython’
[42] * 10
[42, 42, 42, 42, 42, 42, 42, 42, 42, 42]
To check whether a value can be found in a sequence, you use the in operator.
>>> permissions = 'rw' >>> 'w' in permissions True >>> 'x' in permissions False >>> users = ['mlh', 'foo', 'bar'] >>> input('Enter your user name: ') in users Enter your user name: mlh True >>> subject = '\$\$$ Get rich now!!! \$\$$' >>> '\$\$$' in subject True
Deleting elements from a list is easy, too. You can simply use the del statement.
> > > names = [‘Alice’, ‘Beth’, ‘Cecil’, ‘Dee-Dee’, ‘Earl’]
del names[2]
names
[‘Alice’, ‘Beth’, ‘Dee-Dee’, ‘Earl’]
Slicing is a very powerful feature, and it is made even more powerful by the fact that you can assign to slices.
> > > name = list(‘Perl’)
name[1:] = list(‘ython’)
name
[‘P’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’]
Slice assignments can even be used to insert elements without replacing any of the original ones.
> > > numbers = [1, 5]
numbers[1:1] = [2, 3, 4]
numbers
[1, 2, 3, 4, 5]
The append method is used to append an object to the end of a list.it changes the list in place. This means that it does not simply return a new, modified list; instead, it modifies the old one directly.
> > > lst = [1, 2, 3]
lst.append(4)
lst
[1, 2, 3, 4]
The clear method clears the contents of a list, in place.
>>> lst = [1, 2, 3] >>> lst.clear() >>> lst [] It’s similar to the slice assignment lst[:] = [].
If you want a and b to be separate lists, you have to bind b to a copy of a.
>>> a = [1, 2, 3] >>> b = a.copy() >>> b[1] = 4 >>> a [1, 2, 3]
The count method counts the occurrences of an element in a list.
>>> ['to', 'be', 'or', 'not', 'to', 'be'].count('to') 2 >>> x = [[1, 2], 1, 1, [2, 1, [1, 2]]] >>> x.count(1) 2 >>> x.count([1, 2]) 1
The extend method allows you to append several values at once by supplying a sequence of the values you want to append. In other words, your original list has been extended by the other one.
>>> a = [1, 2, 3] >>> b = [4, 5, 6] >>> a.extend(b) >>> a [1, 2, 3, 4, 5, 6]
The index method is used for searching lists to find the index of the first occurrence of a value.
>>> knights = ['We', 'are', 'the', 'knights', 'who', 'say', 'ni'] >>> knights.index('who') 4 >>> knights.index('herring') Traceback (innermost last): File "", line 1, in ? knights.index('herring') ValueError: list.index(x): x not in list
The insert method is used to insert an object into a list.
> > > numbers = [1, 2, 3, 5, 6, 7]
numbers.insert(3, ‘four’)
numbers
[1, 2, 3, ‘four’, 5, 6, 7]
The pop method removes an element (by default, the last one) from the list and returns it.
>>> x = [1, 2, 3] >>> x.pop() 3 >>> x [1, 2] >>> x.pop(0) 1 >>> x [2]
The remove method is used to remove the first occurrence of a value.modifies the list but returns nothing (as opposed to pop).
>>> x = ['to', 'be', 'or', 'not', 'to', 'be'] >>> x.remove('be') >>> x ['to', 'or', 'not', 'to', 'be'] >>> x.remove('bee') Traceback (innermost last): File "", line 1, in ? x.remove('bee') ValueError: list.remove(x): x not in list
The reverse method reverses the elements in the list.it changes the list and does not return anything
> > > x = [1, 2, 3]
x.reverse()
x
[3, 2, 1]
The sort method is used to sort lists in place.
> > > x = [4, 6, 2, 1, 7, 9]
x.sort()
x
[1, 2, 4, 6, 7, 9]
Another way of
getting a sorted copy of a list is using the sorted function.
>>> x = [4, 6, 2, 1, 7, 9] >>> y = sorted(x) >>> x [4, 6, 2, 1, 7, 9] >>> y [1, 2, 4, 6, 7, 9]
The sort method takes two optional arguments: key and reverse.
they available in the sorted function as well.
>>> x = ['aardvark', 'abalone', 'acme', 'add', 'aerate'] >>> x.sort(key=len) >>> x ['add', 'acme', 'aerate', 'abalone', 'aardvark'] >>> x = [4, 6, 2, 1, 7, 9] >>> x.sort(reverse=True) >>> x [9, 7, 6, 4, 2, 1]
Tuples are sequences, just like lists. The only difference is that tuples can’t be changed.
>>> 1, 2, 3 (1, 2, 3) you have to include a comma, even though there is only one value. >>> 42 42 >>> 42, (42,) >>> (42,) (42,)