1/48
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
Lists
mutable collection of data. Typically store homogeneous data, but may store heterogeneous data
Tuples
immutable collection of data. Can hold items of the same or different types
Accessing Elements of a List
Reference a list element by writing the list’s name followed by the element’s index enclosed in [ ] (the subscription operator)
Determining a List’s Length
len(listname)
Lists can be accessed from the end by
using negative indices
Immutable objects
strings and tuples
Appending to a List with +=
Lists can grow dynamically to accommodate new items. When the left operand of += is a list, the right operand must be an iterable- otherwise, a TypeError occurs.
Can concatenate two lists, two tuples, or two strings
using + to create a new sequence of the same type
To create an empty tuple,
use empty parentheses.
Pack a tuple by
separating its values with commas.
What is required when creating a one element tuple?
a comma (element,)
Adding Items to a String or Tuple
+= can be used with strings and tuples, even though they're immutable.
Creates new objects.
Appending Tuples to Lists
listname += tuple
Can unpack any sequence's elements by
assigning the sequence to a comma-separated list of variables (of the appropriate length).
Swapping Values Via Packing and Unpacking
number1, number2 = (number2, number1)
enumerate() and then list() or tuple()
adds a counter to each item in a list or any other iterable
returns a list or tuple of tuples containing the index position and the element for each element of the iterable
slicing
Can slice sequences to create new sequences of the same type containing subsets of the original elements. [start:end+1:step]
[:end]
If you omit the starting index, 0 is assumed
[start:]
Assumes the sequence's length as the ending index.
Specifying a Slice with No Indices
full sequence returned
Slicing with Negative Indices and Steps
sequence is returned backwards
You can modify a list by
assigning to a slice of it—the rest of the list is unchanged
Deleting the Element at a Specific List Index
del list[index]
Deleting a Slice from a List
del list[start:end+1:step]
Deleting a Slice Representing the Entire List
del list[:] empties the list
Deleting a Variable from the Current Session
del listname deletes the list
When you pass a tuple to a function, attempting to modify the tuple’s immutable elements
results in a TypeError
.sort()
sorts a list in ascending order
.sort(reverse=True)
sorts a list in descending order
sorted()
returns a new list containing the sorted elements of its argument sequence. The original sequence is unmodified
Searching
the process of locating a particular key value.
list.index(element)
Searches through a list from index 0 and returns the index of the first element that matches the search key.
ValueError if the value is not in the list.
list.index(element, start)
defines the index where to begin searching for the element’s index
list.index(element, start, end)
defines the range of indices to search for the element’s index
Operator in tests whether its
right operand's iterable contains the left operand's value.
Operator not in tests whether its
right operand's iterable does not contain the left operand's value.
list.insert(index,element)
Inserting an Element at a Specific List Index
list.append(element)
Adding an Element to the End of a List
list.extend(iterable)
Adding All the Elements of a Sequence to the End of a List
list.remove(element)
removes the first occurrence of the element with the specified value
list.clear()
empties a list
list.count(value)
Counting the Number of Occurrences of an Item
list.reverse()
reverses the order of the elements
list.copy()
Method copy returns a new list containing a shallow copy.
list.pop()
By default, the last element is removed. Can also specify the index
List comprehension
[expression for item in iterable if condition]
expression: operation or value to include in the new list.
item: current element from the iterable.
iterable: sequence like a list, tuple, or range.
if condition (optional): filter to include only items that satisfy the condition.
Example of Iterating Backwards Through a Sequence
reversed_numbers = [item ** 2 for item in reversed(list)]
zip(iterator1, iterator2, iterator3 ...)
enables you to iterate over multiple iterables of data at the same time.
Receives any number of iterables and returns an iterator that produces tuples containing the elements at the same index in each.
list[rowindex][columnindex]
Lists can contain other lists as elements.
Typical use is to represent tables of values consisting of information arranged in rows and columns.
To identify a particular table element, we specify two indices-the first identifies the element's row, the second the element's column.