Data Structures Flashcards
What are the basic built-in data structures in Python?
Lists, Tuples, Sequences, Sets, Dictionaries
list.append(x)
Add an item to the end of the list. Equivalent to a[len(a):] = [x].
list.extend(iterable)
Extend the list by appending all the items from the iterable. Equivalent to a[len(a):] = iterable.
list.insert(i, x)
Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).
list.remove(x)
Remove the first item from the list whose value is x. It is an error if there is no such item.
>>> a=[1,2,3,4,2,6,7,8] >>> a [1, 2, 3, 4, 2, 6, 7, 8] >>> a.remove(2) >>> a [1, 3, 4, 2, 6, 7, 8] >>> a.remove(9) Traceback (most recent call last): File "", line 1, in ValueError: list.remove(x): x not in list
list.pop([i])
Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)
list.clear()
Remove all items from the list. Equivalent to del a[:].
list.index(x[, start[, end]])
Return zero-based index in the list of the first item whose value is x. Raises a ValueError if there is no such item.
The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular subsequence of the list. The returned index is computed relative to the beginning of the full sequence rather than the start argument.
>>> a=[1,2,3,4,2,6,7,8] >>> a.index(2) 1 >>> a.index(2,3) 4 >>> a.index(2,8) Traceback (most recent call last): File "", line 1, in ValueError: 2 is not in list
list.count(x)
Return the number of times x appears in the list.
list.sort(key=None, reverse=False)
Sort the items of the list in place (the arguments can be used for sort customization, see sorted() for their explanation). >>> a=[1,10,2,3,4,2,6,7,8] >>> a.sort(key=lambda x: x%2) >>> a [10, 2, 4, 2, 6, 8, 1, 3, 7]
list.reverse()
Reverse the elements of the list in place.
list.copy()
Return a shallow copy of the list. Equivalent to a[:].
list copy is shallow, explain why
?
What do insert, remove or sort return?
They return the default None. This is a design principle for all mutable data structures in Python.
Other languages may return the mutated object, which allows method chaining, such as d->insert(“a”)->remove(“b”)->sort();.
Can we use lists as stack?
Yes. By using append and pop methods.
Can we use lists as queues?
Yes but its slow. Making insertions and deletions to the beginning of a list is slow because all of the other elements have to be shifted by one.
Since using list as queue is slow, what can we use instead?
collections.deque which was designed to have fast appends and pops from both ends.
> > > from collections import deque
queue = deque([“Eric”, “John”, “Michael”])
queue.append(“Terry”) # Terry arrives
queue.append(“Graham”) # Graham arrives
queue.popleft() # The first to arrive now leaves
‘Eric’
queue.popleft() # The second to arrive now leaves
‘John’
queue # Remaining queue in order of arrival
deque([‘Michael’, ‘Terry’, ‘Graham’])
Give examples to create a squares list
1- >>> squares = [] >>> for x in range(10): ... squares.append(x**2) ... >>> squares [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] >>> x 9
NOTE: x variable still exists after loop terminates
2-
squares = list(map(lambda x: x**2, range(10)))
3-
squares = [x**2 for x in range(10)]
> > > [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
and it’s equivalent to:
>>> >>> combs = [] >>> for x in [1,2,3]: ... for y in [3,1,4]: ... if x != y: ... combs.append((x, y)) ... >>> combs [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
> > > vec = [-4, -2, 0, 2, 4]
|»_space;>[x*2 for x in vec]
[-8, -4, 0, 4, 8]