Containers Flashcards

(43 cards)

1
Q

Concatenate two lists

A

li + other_li

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Remove 3’rd element of a list

A

del li[2]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Remove 3rd and 4th element from a list

A

del a[2:4]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Make a copy of a list

A

list.copy()
a[:]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What will list2 = list do?

A

id(list) == id(list2) # => True

Both points to the same object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Check for element 1 in list li

A

1 in li

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

List:
- Add Single element
- Add multiple elements (from an iterable)
- Insert element x at index 1

A
list.append(x)
list.extend(iterable)
list.insert(1, x) # Insert x at index 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

List:
- Remove first occurrence of x
- Remove the item at the given position / last in the list, and return it
- Remove all elements from a list

A
list.remove(x) # Remove first occurrence of x
list.pop([i]) # Remove the item at the given position / last in the list, and return it
list.clear()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

List: Search for element x, starting at 0, ending at index 5 inclusive

A

list.index(x, 0, 6)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Find number of times x appears in the list.

A

list.count(x)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

List comprehension with filtering example

A

[x for x in [3, 4, 5, 6, 7] if x > 5]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to sort and reverse a list?

A
list.sort(key=None, reverse=False)
list.reverse()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are tuples?

A

Like lists but immutable

tup = (1, 2, 3)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to define tuple with single element?

A
type((1))    # => <class 'int'> !!!
type((1,))   # => <class 'tuple'> (add ,)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to define a dictionary?
- Empty
- With some values
- From a list

A
empty_dict = {}
filled_dict = {"one": 1, "two": 2, "three": 3}
dict_from_kv_pairs = dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to get an element from a dictionary? Both methods, how they differ

A

filled_dict["one"]
Throws Key Error when element not found

filled_dict.get("four")
Returns None when element not found

filled_dict.get("four", default)
Returns default when element not found

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Get list of keys and values from a dictionary

A
list(filled_dict.keys())    # => ["one", "two", "three"]
list(filled_dict.values())  # => [1, 2, 3]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Check if one is a key in a dictionary

A

“one” in filled_dict

19
Q

How to update dictionary value under key “one”

A

filled_dict["one"] = 5

20
Q

How to delete dictionary value under key "one"

A

del filled_dict["one"]

21
Q

Dictionary comprehension where each value is a square of its key (for keys 2, 4, 6)

A

{x: x**2 for x in (2, 4, 6)}

22
Q

Define some set with initial values

A

some_set = {1, 1, 2, 2, 3, 4}

23
Q

Define an empty set

A

empty_set = set()

24
Q

Check if value is inside of a set

A

1 in some_set

25
Insert value to a set
`filled_set.add(5)`
26
What are some operations on sets in python?
``` filled_set & other_set # Intersection filled_set | other_set # Union {1, 2, 3, 4} - {2, 3, 5} # Difference => {1, 4} {1, 2, 3, 4} ^ {2, 3, 5} # Symmetric difference => {1, 4, 5} {1, 2} >= {1, 2, 3} # => False ```
27
How to create a set comprehension?
`a = {x for x in 'abracadabra' if x not in 'abc'}`
28
How to iterate a dictionary with a for loop?
``` for k, v in knights.items(): print(k, v) ```
29
How to iterate a list (value + index) with a for loop?
``` for i, v in enumerate(['tic', 'tac', 'toe']): print(i, v) ```
30
How to iterate two lists, one element at the time, simultaneously?
``` for q, a in zip(questions, answers): print('What is your {0}? It is {1}.'.format(q, a)) ```
31
What does `list.index("foo")` return when element was not found?
`ValueError`
32
Get value of key `five` of some dictionary. If it wasnt set yet, set it to 5 and return it.
`filled_dict.setdefault("five", 5)`
33
Make a list where `[1, 2, 3]` is repeated 10 times
`[1, 2, 3] * 10`
34
Get size of a list `s`
`len(s)`
35
Get the smallest element of a list `s`
`min(s)`
36
Get the largest element of a list `s`
`max(s)`
37
Merge two dictionaries
`filled_dict.update({"four": 4})`
38
Unpack dictionary `user_dict` as keyword arguments to a function (like `UserInDB(username, password)`)
`UserInDB(**user_dict)`
39
Create read only set
`frozen_set = frozenset()`
40
How to create double ended queue - What package to import - Add elements to beginning / end - Remove elements to beginning / end
``` from collections import deque d = deque('ghi') d.append('j') d.appendleft('f') d.pop() d.popleft() ```
41
What is namedtuple, how to define and create it
`tuple` with named fields ``` from collections import namedtuple Point = namedtuple('Point', ['x', 'y']) p = Point(11, y=22) ```
42
How to create heap - What package to import - Create heap from existing list - Add element to a heap - Get smallest element from a heap
``` from heapq import heapify, heappop, heappush data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0] heapify(data) # rearrange the list into heap order heappush(data, -5) # add a new entry [heappop(data) for i in range(3)] # fetch the three smallest entries ```
43
How to create enum? - What package to import - Two methods of defining enum - How to use it
``` from enum import Enum // Class syntax class Color(Enum): RED = 1 GREEN = 2 BLUE = 3 // Functional syntax Color = Enum('Color', [('RED', 1), ('GREEN', 2), ('BLUE', 3)]) // Use Color.RED ```