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]