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’]
An optional argument called a “ “ specifies which characters to use as word boundaries.
delimiter
” “ is the inverse of split. It takes a list of strings and concatenates the elements.
join
To check whether two variables refer to the same object, you can use the “ “ operator.
is
If two objects are “ “, they are also equivalent, but if they are equivalent, they are not necessarily “ “.
identical
If another list has the same elements, we say it has the
same value, but it is not the same “ “.
object
The association of a variable with an object is called a “ “.
reference
An object with more than one reference has more than one name, so we say that the object is “ “.
aliased
If the aliased object is “ “, changes made with one alias affect the other.
mutable
Before using list “ “ and operators, you should read the documentation carefully and then test them in interactive mode.
methods
Define list:
A sequence of values
Define element:
One of the values in a list (or other sequence), also called items
A processing pattern that traverses a sequence and performs an operation on each element is called what?
map
A processing pattern that traverses a list and selects the elements that satisfy some criterion.
filter
Something a variable can refer to…
**object. An object has a type and a value. **
A circumstance where two or more variables refer to the same object is called?
Aliasing