Chapter 7: Lists and Dictionaires Flashcards

(58 cards)

1
Q

container

A

a construct used to group related values together and contains references to other objects instead of data
Ex. A list

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

list

A

a container with brackets: []
contains elements

Are Mutable/ Changeable

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

element

A

a list item

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

index

A

a position inside of a list

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

method

A

adds or removes elements to/ from a list

Ex: .append()
.pop()
.remove()

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

.append()

A

list method is used to add new elements to a list.

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

.pop()

A

A list method that removes elements from a list

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

.remove()

A

A list method that removes elements from a list

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

len(list)

A

Find the length of the list

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

list1 + list2

A

Produce a new list by concatenating list2 to the end of list1

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

min(list)

A

Find the element in list with the smallest value. All elements must be of the same type

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

max(list)

A

Find the element in list with the largest value. All elements must be of the same type.

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

sum(list)

A

Find the sum of all elements of a list (numbers only)

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

list.index(val)

A

Find the index of the first element in list whose value matches val.

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

list.count(val)

A

Count the number of occurrences of the value val in list.

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

set()

A

an unordered collection of unique elements

Any repeats will only occur once

Are mutable/ changeable

Must enter a list; MUST HAVE BRACKETS!

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

set_literal = {}

A

Another way of writing a set

Has CURLY Brackets

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

set1.update(set2)

A

Adds the elements in set2 to set1.

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

set.clear()

A

Clears all elements from the set

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

set.intersection(set_a, set_b, set_c…)

A

Returns a new set containing only the elements in common between set and all provided sets.

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

set.union(set_a, set_b, set_c…)

A

Returns a new set containing all of the unique elements in all sets.

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

set.difference(set_a, set_b, set_c…)

A

Returns a set containing only the elements of set that are not found in any of the provided sets.

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

set_a.symmetric_difference(set_b)

A

Returns a set containing only elements that appear in exactly one of set_a or set_b

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

.add() vs .update()

A

.add()
Adds a single element to a list or set
.update()
Adds multiple elements to a set

25
.remove() vs .pop()
.remove() deletes a certain VALUE .pop() deletes a certain INDEX
26
my_list = [1, 2, 3] | print(my_list)
Creates a list. [1, 2, 3]
27
my_list = list('123') | print(my_list)
Creates a list ['1', '2', '3']
28
my_list = [1, 2, 3] | print(my_list[1])
Get an element from a list 2
29
my_list[start:end] Ex: my_list = [1, 2, 3] print(my_list[1:3])
Get a new list containing some of another list's elements. [2, 3]
30
my_list1 + my_list2 Ex: my_list = [1, 2] + [3] print(my_list)
Get a new list with elements of my_list2 added to end of my_list1. [1, 2, 3]
31
my_list[i] = x Ex: my_list = [1, 2, 3] my_list[2] = 9 print(my_list)
Change the value of the ith element in-place. [1, 2. 9]
32
del my_list[i] Ex: my_list = [1, 2, 3] del my_list[1] print(my_list)
Delete an element from a list. [1, 3]
33
list.append(x) Ex: my_list = [5, 8] my_list.append(16)
Add an item to the end of list. [5, 8, 16]
34
extend([x]) Ex: my_list = [5, 8] my_list.extend([4, 12])
Add all items in [x] to list. [5, 8, 4, 12]
35
list.insert(i, x) Ex: my_list = [5, 8] my_list.insert(1, 1.7)
Insert x into list before position i. Basically: i = index value/ where you want your x x = what you want to add to the list [5, 1.7, 8]
36
list.remove(x) Ex: my_list = [5, 8, 14] my_list.remove(8)
Remove first item from list with value x. [5, 14]
37
list.pop(i) Ex: my_list = [5, 8, 14] val = my_list.pop(0)
Remove and return item at position i in list. [8, 14] val is 5 By Default: i = -1 AKA: the last item/ element
38
list.sort() Ex: my_list = [14, 5, 8] my_list.sort()
Sort the items of list in-place. [5, 8, 14] Increasing/ lowest to highest Alphabetical
39
list.reverse() Ex: my_list = [14, 5, 8] my_list.reverse()
Reverse the elements of list in-place. [8, 5, 14]
40
list.index(x) Ex: my_list = [5, 8, 14] print(my_list.index(14))
Return index of first item in list with value x. Basically: x = the value which you want the index 2
41
list.count(x) Ex: my_list = [5, 8, 5, 5, 14] print(my_list.count(5))
Count the number of times value x is in list. 3
42
in vs not in
operators that can be used in if else statements in checks if there is something in something not in checks if there is not something in something
43
is vs is not
An identity operator that check whether two operands are bound to a single object
44
for loop
loops over each element in a container one at a time Syntax: for in :
45
enumerate()
iterates over a list and provides an iteration counter
46
all(list) Ex: print(all([1, 2, 3])) print(all([0, 1, 2]))
True if every element in list is True (!= 0), or if the list is empty. True False
47
any(list) Ex: print(any([0, 2])) print(any([0, 0]))
True if any element in the list is True. True False
48
my_list[start:end] Ex: my_list = [5, 10, 20] print(my_list[0:2])
Get a list from start to end (minus 1); end is exclusive [5, 10]
49
my_list[start:end:stride] Ex: my_list = [5, 10, 20, 40, 80] print(my_list[0:5:3])
Get a list of every stride element from start to end (minus 1) The stride is how many elements are skipped in between [5, 40]
50
my_list[start:] Ex: my_list = [5, 10, 20, 40, 80] print(my_list[2:])
Get a list from start to end of the list. [20, 40, 80]
51
my_list[:end] Ex: my_list = [5, 10, 20, 40, 80] print(my_list[:4])
Get a list from beginning of list to end (minus 1). [5, 10, 20, 40]
52
my_list[:] Ex: my_list = [5, 10, 20, 40, 80] print(my_list[:])
Get a copy of the list. [5, 10, 20, 40, 80]
53
dictionary
Like lists but have pairs; kinda how dictionaries have words and a definition assigned to them. Represented as: dict Mutable syntax: = {} HAS CURLY BRACKETS
54
key
a term that can be located in a dictionary such as the word "cat" in the English dictionary
55
value
describes some data associated with a key, such as a definition
56
key:value pairs Ex: players = {'Lionel Messi': 10, 'Cristiano Ronaldo': 7}
creates a dictionary called players with two keys: 'Lionel Messi' and 'Cristiano Ronaldo', associated with the values 10 and 7 (their respective jersey numbers)
57
dict[k] = v:
Adds the new key-value pair k-v, if dict[k] does not already exist. Updates the existing entry dict[k], if dict[k] already exists.
58
del dict[k]
Deletes the entry dict[k]