IttP 2 - Dictionaries Flashcards

1
Q

3 main collections of data or data structures in Python are - (3)

A
  1. Lists
  2. Tuples
  3. Dictionaries
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Wha are lists? - (2)

A
  • Lists are used to store collecitons of elements in Python
  • Lists are mutable, meaning you can add, remove, or modify elements after the list is created.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is similar about lists and tuples? 0 (2)

A

Lists and tuples are both used to store collections of elements in Python

You can access elements of both lists and tuples using indexing (e.g., print(my_list[0]) and slicing (e.g, start:stop:step)

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

What is differences about lists and tuples? - (4)

A

Lists are mutable, meaning you can modify, add, or remove elements after the list is created.

Tuples are immutable, meaning once they are created, their elements cannot be changed, added, or removed.

Lists are denoted by square brackets [ ].

Tuples are denoted by parentheses ( ).

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

How is lists defined? - (2)

A

defined with square brackets

(e.g.,a=[1,2,3]).

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

H

How is tuples defined? - (2)

A

defined with round brackets

e.g., (a=(1,2,3))

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

What are dictitonaries?

A

Dictionaries in Python serve as a powerful data structure for storing mappings of key-value pairs

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

In real world, dicitonaries link words to defintiions

The equivalent to “words” in Python dictionaries are called… and the “definitions” are called - (2)

A

The equivalent to “words” in Python dictionaries are called keys and the “definitions” are called values.

So “words” → “definitions” maps to keys → values.

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

Unlike real dictionaries where one word can have many meanings, Python dictionaries,

A

Each key can only have a single value

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

Example that only Python dicionaries, each value can have a single value associated wih it which differs from real-world dictionaires where one word can have multiple meanings - (2)

A

For example, in a real-world dictionary, the word “bank” can refer to a financial institution as well as the edge of a river.

However, in a Python dictionary, each key is unique, and thus, the key “bank” could only be associated with a single value. the dictionary associates each key (“bank” and “river_bank”) with a single value.

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

How are Python dicionaries defined?

A

defined using curly braces ‘{ }’

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

Whatt does dicitonaries consist of inside curly braces? ‘{}’? - (2)

A

consist of comma-separated key-value pairs.

Each key-value pair is separated by a colon :

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

What is general syntax of dicitonaries in Python? - (3)

A

my_dict is the name of the dictionary variable.

key1, key2, key3, etc., are the keys of the dictionary.

value1, value2, value3, etc., are the corresponding values associated with the keys.

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

The type of a dictionary is..

A

dict

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

Write a code that has an empty dicitonary in variable my_dict

Print contents of my_dict
Print type of conttentts of my_dict

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

What would be the output of this code?

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

When we print the (empty) dictionary, we see thatt

A

Python uses the curly braces { } to indicate that we are printing a dictionary.

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

To add someitems to Python dictionaries, we use the same.. - (2)

A

same indexing notation we saw in the list examples in the previous session : square brackets: [ ].

Python always uses square brackets for indexing across different data structures such as lists, typles; the different bracket styles are only use in the initial creation of the variable (e.g., curly braces for dictionaries and parenthses ‘()’ for tuples)

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

For example,

How to add key of mykey and assign value of 20 in empty dictonary called my_dict?

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

Write a code that has

my_dict variable with empty dictionary

Assign key of ‘mykey’ to value 20
Assign key of ‘another key’ to value 100
Assign key of ‘Averylongkeyblahblahblah’ to value 1
Assign key ‘Navin R Johnson’ to value ‘2513. Elm St’

Prints dicionary my_dict
Prints out length of string using f-string

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

What will be output of this code?

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

Most Python data types can be used as dictionary keys.

This includes

A

strings, numbers (integers and floats) and tuples

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

Altthough most Python data types (e.g., strings, numbers, tuples) can be used as dictionary keys

Mutable types like lists can not be used as keys because..

A

they are not hashable (i.e., their values can change, which would disrupt the internal structure of the dictionary).

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

In Python dicionaries, any Python data type can be used as - (2)

A

a value

This includes integers, floats, strings, lists, tuples, dictionaries etc…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Note in this code when we print the dicionary it,
prints the entries we added to dictionary in order which we added them
26
What happens if you attempt to assign multiple values to the same key in dicionary?
we replace the existing value in the dictionary.
27
Example of adding add a key-value pair to a dictionary and then add another value with the same key
In this part of the code, you add a new value 2000000 to the dictionary with the same key 'mykey'. This operation updates the value associated with the key 'mykey' to 2000000, effectively overwriting/replacing the previous value in the dictionary
28
To extract individual values from a dictionary, you use the
same index (square bracket) notation that we use to add the entries in dicttionary and used in lists to extract items:
29
Write a code that has empty dicionary stored in variable 'my_dict' Adds key of 'mykey' assigned to value 20 in 'my_dict' Retrieves value associated with key 'mykey' from dicionary 'my_dict' in variable called'test_var' Prints 'test_var'
30
What is output of code?
31
When When you try to access the value associated with a key in a dictionary that doesn't exist Python raises an....
Key Error exception since Python cannot find the specified key in the dictionary.
32
Example of When you try to access the value associated with a key in a dictionary that doesn't exist
33
To handle cases where we would like to extract value from a dicionary and if key may not exist in dicionary, without having to deal with raising (a KeyError) exception error, you can use the
get() method of dictionaries, which allows you to specify a default value to return if the key is not found:
34
Write a code that has empty dictionary stored in variable called 'my_dict' Add a key called 'mykey' with value 1000 to dictionary called 'my_dict' Then retrieves value associated with key 'mykey' from dicionary 'my_dict' using get() and assigns to variable called'test_var' Prints 'test_var' Then retrieve value associated with non-existent key called 'mykey_notthere' using get() without specificying default value and assigns to variable my_value Prints type of my_value and prints its contents
35
What is output of this code?
36
By default, the get() function returns 'None' if there is no
relevant key in dictionary instead of raising a KeyError exception,
37
Example of specificizing argument in get()
my_dict.get('z', 'Key not found') returns 'Key not found' because 'z' is not a key in the dictionary, and the default value 'Key not found' is provided as the second argument.
38
Just like lists, dictionaries have methods to delete items such as - (2)
pop() function del statement
39
As with lists, pop() function in dictionary - (2)
It removes the item from dictionary (i.e., key-value pair associated with that key from dictionary) returns its value This allows you to use the value after removing it from the dictionary.
40
As with lists, del statement in dictionary - (2)
removes the item (i.e., a key-value pair) with the specified key from the dictionary without returning its value. It directly modifies the dictionary.
41
What are the similarities between pop() and del statement? - (2)
Both del statement and .pop() method are used to remove items from dictionaries. They both allow you to specify the key of the item you want to remove from the dictionary or (specify index in list)
42
What are the differences between pop() and del statement? - (2)
del statement removes the item with the specified key from the dictionary without returning its value. Whereas pop() function removes the item from dictionary returns its value
43
Write a code that: creates a dictionary called my_new_dict with key of 'age' associated with value fo 35, key of 'test_value' associated with value of 1000 Prints my_new_dict Removing the key 'age' and its value as well as storing into variable called removed_value using pop Prints removed_value Print my_new_dict
44
What is output of this code?
45
Write a code that: creates a dictionary called my_new_dict with key of 'age' associated with value fo 35, key of 'test_value' associated with value of 1000 Prints my_new_dict Remove key of 'test value' and its value of 1000 using del statement Then prints my_new_dict
46
Coding exercise Variable 'ages' which has dictionary of key in which P1 is associated with value 35 P2 is associated with value 38 P3 is associated with value 21 P4 is associated with value 28 P5 is associated with value 33 Extract a single value associated with key 'P5' from dictionary called 'ages'
47
What is output of the code?
33
48
In Python, we can check for the existence of keys so to check if a single specific key exists in a dictionary we can use the
the in keyword followed by key you want to check and the dictionary name
49
In Python we can check the existence of a single key in a dictionary example syntax
For example P1 in ages will return True if P1 is a key in ages
50
Write a code: Variable 'ages' which has dictionary of key in which P1 is associated with value 35 P2 is associated with value 38 P3 is associated with value 21 P4 is associated with value 28 P5 is associated with value 33 print using formatted string to check is P1 key in ages dictionary? print using formatted string to check is P1000 key in ages dictionary?
51
What is output of code?
52
In dictionaries, to get all the keys in the dictionary we can use the
the .keys() method
53
In key() method, it returns a
a special type called dict_keys, which represents a view of all the keys in the dictionary
54
Write a code: Variable 'ages' which has dictionary of key in which P1 is associated with value 35 P2 is associated with value 38 P3 is associated with value 21 P4 is associated with value 28 P5 is associated with value 33 print using formatted string to check is P1 key in ages dictionary? print all the keys in ages dictionary and the type
55
What is the output of this code?
56
Although in this output the keys output in the order we entered them
this is not guaranteed by Python and that you may need to sort the keys
57
You can convert keys of dictionary (using .key() ) / values of dictionary (using .values() function) to a list by
by simply 'casting' (converting) the return value to the list type:
58
Write a code that: Write a code: Variable 'ages' which has dictionary of key in which P1 is associated with value 35 P2 is associated with value 38 P3 is associated with value 21 P4 is associated with value 28 P5 is associated with value 33 has variable 'my_keys' and converts keys of age dictionary into a list prints type of my_keys prints my_keys prints length of my_keys
59
What is output
60
What does .values() function do to a dictionary?
It just gets the values of a dictionary
61
What type of object does .values() return?
a dict_values object,
62
Write a code that: Write a code: Variable 'ages' which has dictionary of key in which P1 is associated with value 35 P2 is associated with value 38 P3 is associated with value 21 P4 is associated with value 28 P5 is associated with value 33 prints just the values of ages dictionary prints type of age.values Cast values from ages dictionary into a list and store in variable called my_vals and print it
63
What is the output?
64
What xxxx thing to replace in code?
65
We often want to loop over dictionaries to do something to each key/value pair. different methods of iterating over dictionaries in Python and accessing key-value pairs during iteration are... - (2)
1. Looping over keys 2. Using .items() for Key-Value Pairs
66
Looping over Keys method is where looping over the keys of the dictionary. The default behaviour of dictionary when used in for loop is to
The default behaviour of a dictionary when being used in a for loop is to iterate over the keys:
67
Example of the default behaviour of a dictionary when being used in a for loop is to iterate over the keys: - (2)
In this example, the for loop iterates over the dictionary ages. During each iteration, the loop variable key represents each key in the dictionary ages.
68
Write a piece of code that Variable 'ages' which has dictionary of key in which P1 is associated with value 35 P2 is associated with value 38 P3 is associated with value 21 P4 is associated with value 28 P5 is associated with value 33 Print out each key and value it has in dictionary using for loop ('loop over keys of dictionary' method)
69
What is output of this code?
70
we need both the key and the value on each loop iteration, we can get this without needing to use the dict[key] syntax by using the
.items() iterator. Items returns key/value pairs one at a time...
71
The .items() iterator returns a
key/value pairs one at a time...
72
Write a piece of code that: Variable 'ages' which has dictionary of key in which P1 is associated with value 35 P2 is associated with value 38 P3 is associated with value 21 P4 is associated with value 28 P5 is associated with value 33 Print out each key and value it has in dictionary using for loop and using .items() iterator ('items()' method)
73
What is interesting about this code?
What's also interesting here is that `items` returns two values and they are automatically assigned to the two loop variables keyThing and valThing. How convenient!
74
What is output of the code?
75
different methods of iterating over dictionaries in Python and accessing key-value pairs during iteration are 1. Looping over keys 2. Using .items() for Key-Value Pairs Both achieve the same thing but use
slightly different syntax
76
different methods of iterating over dictionaries in Python and accessing key-value pairs during iteration are 1. Looping over keys 2. Using .items() for Key-Value Pairs Which method you will choose will depend on context - (3)
For instance, if you need to sort the keys so that you work through the dictionary in a specific order, you might use a modification of the first version: for k in sorted(ages): This loop would guarantee that we walk through the keys in a sorted order
77
You are making a database for your experiment:In the code below you will: 1: Create an empty dictionary which will be used to map IDs to ages. 2: Write a loop which goes around three times and, each time, lets the user input an ID or name and then an age. Each time, add a new entry to the dictionary. 3: Print out the dictionary using the normal print command. Then try printing the dictionary, one item at a time, after sorting the names alphabetically. Try and make this second output look as neat as possible using the formatting rules we discussed in the previous session. Useful keywords that are used here: input range print print(f"") sort for .... in .... sorted(...)
78
What is output of code if: Participant ID:1090 Age?22 Participant ID:8000 Age?50 Participant ID:50000 Age?60