Lists - Chapter 10 Flashcards
Are lists mutable or immutable?
Mutable
The values in a list are called “ “ or sometimes “ “.
elements or items.
A list within another list is called?
Nested
A list with no elements is called?
An Empty List
If you need to write or update the elements in a list using indices. What built-in functions could you use?
range and len
for i in range(len(list)):
list[i] = list[i] * 2
Although a list can contain another list, the nested list still counts as a single….
Element
What will the outcome of this be?
a = [1, 2, 3]
b = [4, 5, 6]
c = a + b
print(c)
[1, 2, 3, 4, 5, 6]
What will the outcome of this be?
[1, 2, 3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
t = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’,]
What happens if we do the following?
t[:]
The slice will be a complete copy of the list.
What will the following output be?
t = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]
t[1:3] = [‘x’, ‘y’]
print(t)
[‘a’, ‘x’, ‘y’, ‘d’, ‘e’, ‘f’]
This method takes a list as an argument and appends all of the elements.
**list.extend()
»> t1 = [1, 2, 3]
»> t2 = [4, 5, 6]
»> t1.extend(t2)
»> t1
[1, 2, 3, 4, 5, 6] **
” “ arranges the elements of a list from low to high.
**list.sort()
»> t = [‘c’, ‘d’, ‘a’, ‘b’, ‘e’]
»> t.sort()
»> t
[‘a’, ‘b’, ‘c’, ‘d’, ‘e’] **
Most list methods are “ “; they modify the list and return “ “
void and return None
Give an example of an augmented assignment statement
variable += x
Which is equivalent to
** variable = variable + x**
A variable used to collect the sum of the elements is sometimes referred to as?
Accumulator
def capitalize(t):
res = []
for s in t:
res.append(s.capitalize())
return res
In this instance, res is the accumulator.
An operation that changes a sequence of elements into a single value is sometimes called?
reduce
What is the following operation called?
def only_upper(t):
res = []
for s in t:
if s.isupper():
res.append(s)
return res
A filter
Most common list operations can be expressed as a combination of which 3 things?
map, filter, reduce
” “ modifies the list and returns the element that was removed.
variable.pop()
»> t = [‘p’, ‘o’, ‘p’]
»> x = t.pop(1)
»> t
[‘p’]
If you do not provide an index for pop, what happens?
»> t = [‘p’, ‘o’, ‘p’]
»> x = t.pop()
»> t
The last element is removed.
»> x
[‘p’]
What is the return value for remove?
None
A string is a sequence of characters and a list is a sequence of?
Values
To convert from a string to a list, you can use?
list
»> b = ‘batman’
»> j = list(b)
»> j
[‘b’, ‘a’, ‘t’, ‘m’, ‘a’, ‘n’]
The list function breaks a string into individual letters. If you want to break a string into words, what should you use?
variable.split()
»> s = ‘pining for the fjords’
»> t = s.split()
»> t
[‘pining’, ‘for’, ‘the’, ‘fjords’]