Chapter 7: Lists and Dictionaires Flashcards

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
Q

.remove() vs .pop()

A

.remove()
deletes a certain VALUE
.pop()
deletes a certain INDEX

26
Q

my_list = [1, 2, 3]

print(my_list)

A

Creates a list.

[1, 2, 3]

27
Q

my_list = list(‘123’)

print(my_list)

A

Creates a list

[‘1’, ‘2’, ‘3’]

28
Q

my_list = [1, 2, 3]

print(my_list[1])

A

Get an element from a list

2

29
Q

my_list[start:end]

Ex:
my_list = [1, 2, 3]
print(my_list[1:3])

A

Get a new list containing some of another list’s elements.

[2, 3]

30
Q

my_list1 + my_list2

Ex:
my_list = [1, 2] + [3]
print(my_list)

A

Get a new list with elements of my_list2 added to end of my_list1.

[1, 2, 3]

31
Q

my_list[i] = x

Ex:
my_list = [1, 2, 3]
my_list[2] = 9
print(my_list)

A

Change the value of the ith element in-place.

[1, 2. 9]

32
Q

del my_list[i]

Ex:
my_list = [1, 2, 3]
del my_list[1]
print(my_list)

A

Delete an element from a list.

[1, 3]

33
Q

list.append(x)

Ex:
my_list = [5, 8]
my_list.append(16)

A

Add an item to the end of list.

[5, 8, 16]

34
Q

extend([x])

Ex:
my_list = [5, 8]
my_list.extend([4, 12])

A

Add all items in [x] to list.

[5, 8, 4, 12]

35
Q

list.insert(i, x)

Ex:
my_list = [5, 8]
my_list.insert(1, 1.7)

A

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
Q

list.remove(x)

Ex:
my_list = [5, 8, 14]
my_list.remove(8)

A

Remove first item from list with value x.

[5, 14]

37
Q

list.pop(i)

Ex:
my_list = [5, 8, 14]
val = my_list.pop(0)

A

Remove and return item at position i in list.

[8, 14]
val is 5

By Default:
i = -1
AKA: the last item/ element

38
Q

list.sort()

Ex:
my_list = [14, 5, 8]
my_list.sort()

A

Sort the items of list in-place.

[5, 8, 14]

Increasing/ lowest to highest
Alphabetical

39
Q

list.reverse()

Ex:
my_list = [14, 5, 8]
my_list.reverse()

A

Reverse the elements of list in-place.

[8, 5, 14]

40
Q

list.index(x)

Ex:
my_list = [5, 8, 14]
print(my_list.index(14))

A

Return index of first item in list with value x.
Basically:
x = the value which you want the index

2

41
Q

list.count(x)

Ex:
my_list = [5, 8, 5, 5, 14]
print(my_list.count(5))

A

Count the number of times value x is in list.

3

42
Q

in vs not in

A

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
Q

is vs is not

A

An identity operator that check whether two operands are bound to a single object

44
Q

for loop

A

loops over each element in a container one at a time

Syntax:
for in :

45
Q

enumerate()

A

iterates over a list and provides an iteration counter

46
Q

all(list)

Ex:
print(all([1, 2, 3]))
print(all([0, 1, 2]))

A

True if every element in list is True (!= 0), or if the list is empty.

True
False

47
Q

any(list)

Ex:
print(any([0, 2]))
print(any([0, 0]))

A

True if any element in the list is True.

True
False

48
Q

my_list[start:end]

Ex:
my_list = [5, 10, 20]
print(my_list[0:2])

A

Get a list from start to end (minus 1); end is exclusive

[5, 10]

49
Q

my_list[start:end:stride]

Ex:
my_list = [5, 10, 20, 40, 80]
print(my_list[0:5:3])

A

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
Q

my_list[start:]

Ex:
my_list = [5, 10, 20, 40, 80]
print(my_list[2:])

A

Get a list from start to end of the list.

[20, 40, 80]

51
Q

my_list[:end]

Ex:
my_list = [5, 10, 20, 40, 80]
print(my_list[:4])

A

Get a list from beginning of list to end (minus 1).

[5, 10, 20, 40]

52
Q

my_list[:]

Ex:
my_list = [5, 10, 20, 40, 80]
print(my_list[:])

A

Get a copy of the list.

[5, 10, 20, 40, 80]

53
Q

dictionary

A

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
Q

key

A

a term that can be located in a dictionary such as the word “cat” in the English dictionary

55
Q

value

A

describes some data associated with a key, such as a definition

56
Q

key:value pairs

Ex:
players = {‘Lionel Messi’: 10, ‘Cristiano Ronaldo’: 7}

A

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
Q

dict[k] = v:

A

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
Q

del dict[k]

A

Deletes the entry dict[k]