chapter 2 Lists Flashcards

1
Q

If a function call returns a sequence, you can index it directly.

A

> > > fourth = input(‘Year: ‘)[3]
Year: 2005
fourth
‘5’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When slicing, you specify (either explicitly or implicitly) the start and end points of the slice. Another parameter,
which normally is left implicit

A

> > > numbers[0:10:1]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

if you want every fourth element of a sequence, you need to supply only a step size of four.

A

> > > numbers[::4]

[1, 5, 9]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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.

A
>>> 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]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Sequences can be concatenated with the addition (plus) operator. you cannot concatenate sequences of different types.

A
>>> [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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Multiplying a sequence by a number x creates a new sequence where the original sequence is repeated x times:

A

> > > ‘python’ * 5
‘pythonpythonpythonpythonpython’
[42] * 10
[42, 42, 42, 42, 42, 42, 42, 42, 42, 42]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

To check whether a value can be found in a sequence, you use the in operator.

A
>>> 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Deleting elements from a list is easy, too. You can simply use the del statement.

A

> > > names = [‘Alice’, ‘Beth’, ‘Cecil’, ‘Dee-Dee’, ‘Earl’]
del names[2]
names
[‘Alice’, ‘Beth’, ‘Dee-Dee’, ‘Earl’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Slicing is a very powerful feature, and it is made even more powerful by the fact that you can assign to slices.

A

> > > name = list(‘Perl’)
name[1:] = list(‘ython’)
name
[‘P’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Slice assignments can even be used to insert elements without replacing any of the original ones.

A

> > > numbers = [1, 5]
numbers[1:1] = [2, 3, 4]
numbers
[1, 2, 3, 4, 5]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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.

A

> > > lst = [1, 2, 3]
lst.append(4)
lst
[1, 2, 3, 4]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

The clear method clears the contents of a list, in place.

A
>>> lst = [1, 2, 3]
>>> lst.clear()
>>> lst
[]
It’s similar to the slice assignment lst[:] = [].
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

If you want a and b to be separate lists, you have to bind b to a copy of a.

A
>>> a = [1, 2, 3]
>>> b = a.copy()
>>> b[1] = 4
>>> a
[1, 2, 3]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

The count method counts the occurrences of an element in a list.

A
>>> ['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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> a.extend(b)
>>> a
[1, 2, 3, 4, 5, 6]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

The index method is used for searching lists to find the index of the first occurrence of a value.

A
>>> 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
17
Q

The insert method is used to insert an object into a list.

A

> > > numbers = [1, 2, 3, 5, 6, 7]
numbers.insert(3, ‘four’)
numbers
[1, 2, 3, ‘four’, 5, 6, 7]

18
Q

The pop method removes an element (by default, the last one) from the list and returns it.

A
>>> x = [1, 2, 3]
>>> x.pop()
3
>>> x
[1, 2]
>>> x.pop(0)
1
>>> x
[2]
19
Q

The remove method is used to remove the first occurrence of a value.modifies the list but returns nothing (as opposed to pop).

A
>>> 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
20
Q

The reverse method reverses the elements in the list.it changes the list and does not return anything

A

> > > x = [1, 2, 3]
x.reverse()
x
[3, 2, 1]

21
Q

The sort method is used to sort lists in place.

A

> > > x = [4, 6, 2, 1, 7, 9]
x.sort()
x
[1, 2, 4, 6, 7, 9]

22
Q

Another way of

getting a sorted copy of a list is using the sorted function.

A
>>> x = [4, 6, 2, 1, 7, 9]
>>> y = sorted(x)
>>> x
[4, 6, 2, 1, 7, 9]
>>> y
[1, 2, 4, 6, 7, 9]
23
Q

The sort method takes two optional arguments: key and reverse.
they available in the sorted function as well.

A
>>> 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]
24
Q

Tuples are sequences, just like lists. The only difference is that tuples can’t be changed.

A
>>> 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,)