Module 9 - Lists and Tuples Flashcards
What is a sequence? Name two types of sequences Python provides.
An object that contains multiple items of data
The generic term for an ordered set
Lists and tuples are two (of many) types of sequences
What’s an object?
Anything that can contain information: a variable, a file, a string..
What’s the difference between a list and tuple?
A list is mutable and tuple is immutable (not changeable)
What’s a list? What’s an element?
A list is a sequence of values, an object that contains multiple data items
An element is an item in a list
What’s the format/ syntax for a list in Python?
list = [item1, item2, etc.]
T or F: An error will occur if all items in a list are not the same type.
False - a list can hold different types of items
How do you display a list? How do you convert certain objects to lists?
print function to display
list () function to convert to list
How do you make multiple copies of a list?
Repetition operator: makes multiple copies of a list and joins them together
list * n
What is the result of [1,2] * 4?
[2] + [4,7]?
[1,2,1,2,1,2,1,2]
[2,4,7]
How do you iterate over a list? What is the syntax?
you can use a for-loop!
for x in list:
(same way we did strings!)
What’s an index (w.r.t. lists)?
A number specifying the position of an element in a list
What’s the index of the first element? The nth element? The last element?
Index of first element is always 0, nth element has an index of n-1, last element is -1
(can use negative indexes to identify positions relate to the end of the list)
What’s a good way to think about element vs. index numbering?
Think of index as location:
Count from 0!
The first element has a LOCATION (index) of 0
The nth element has a location of n-1
How do you find the length of a list? How do you find index from length?
can use len(list) function
index of the last element is len (list) - 1
What happens if you iterate over a list w/ a loop and use an invalid index?
IndexError
To avoid this, be sure to use len( ) - 1 as the last index iteration
What is an exception?
AKA runtime error
The error does not appear until after the program has started running
(e.g., type code to iterate over a list, but the index is outside the boundary of the list)
How do you assign a new value to an existing list element (e.g., the second element)?
list [1] = new_value
*must be valid index!
T or F: Only lists of the same type can be concatenated.
False (Python 3)
(this was True in earlier versions of Python)
Can do: [1,2] + [‘a’,’e’]
Can also use +=
If you assign: z=x+y, then you change x, what happens to z?
z remains old x+y (NOT the updated x value!)
*IMPORTANT!
What is a slice?
A span of items that are taken from a sequence
What is the list slicing format?
list[start : end]
Span is a list containing copies of elements from start up to, but not including, end
What happens if start or end not specified in a span/slice?
If start not specified, 0 is used for start index
If end not specified, len(list) is used for end index
Can slicing expressions include a step value or negative index?
Yes (negative indexes relative to end of list)
X = [1, 3, 5, 2, 9]
what do the following return?
X [-4:-2]
X [-1:2]
[3,5]
[ ]
X = [1, 3, 5, 2, 9]
what do the following return?
X [:1]
X [3:]
[1]
[2,9]
**REMEMBER first element is INDEX 0!!
Which operator can you use to determine whether an item is contained in a list? Not contained?
‘in’ operator
‘not in’
general format: item in list
What kind of function is ‘in’?
Logical function
It returns True or False
What is the result of “[5,1] in [2,5,1,7]”? Why?
False
Because [5,1] as a list does not appear in the list [2,5,1,7]
Each element of the list [2,5,1,7] is its own entity and the ‘in’ operator searches for 5,1 as ONE entity (list)
What is the result of ‘ac’ in ‘back’? Why?
True
strings do not follow the same rules as lists - ac is clearly in back
How do you search if [5,1] is in [2,5,1,7]?
You will need a for loop to check each element of [5,1] whether they appear in the bigger list and in that order
Code used to add items to a list – item is appended to the end of the existing list
append(item)
if X = [1,3,5] and you wanted to add 4:
X.append(4)
[1,3,5,4]
Code used to determine where an item is located in a list
index(item)
Returns the index of the first element in the list containing item
Raises ValueError exception if item not in the list
Code used to insert ‘item’ at position ‘index’ in the list
insert(index, item)
List expands in size, indices are shifted accordingly
No exception if you specify invalid index - if index beyond the end, item added to the end (if negative that’s beyond, added to beginning)
Code used to sort the elements of the list in ascending order (from lowest to highest value)
sort( )
Code removes the first occurrence of ‘item’ in the list
remove(item)
ValueError exception if item not in list
Code that reverses the order of the elements in the list
reverse( )
Code that removes an element from a specific index in a list
del statement
general format: del list[i]
What are the min and max functions? How do they work?
Built-in functions that returns the item that has the lowest or highest value in a sequence
The sequence is passed as an argument
max (sequence)