M03 - Dictionaries Flashcards

1
Q

Dictionary definition + contents

A
  • Object that stores a collection of data

- Has a key and a value or key-value pairs (think dictionary where there’s Word-Definition)

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

Key-Value Pairs example with 2 keys/how a dictionary prints out

A

{‘key1’ : value1 , ‘key2’ : value2 , etc.}

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

2 Important Rules for Dictionaries

A
  1. Keys must be immutable objects, like integers, floating-point decimals, or strings. Keys cannot be lists or any other time of mutable object
  2. Values in a dictionary can be objects of any type: integers, floating-point decimals, strings, Boolean values, datetime values, and lists
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Initialize/Create an empty dictionary options

A

dict_variable = { }

dict_variable = dict( )

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

Standard format for creating a key in a dictionary and syntax example

A

-Place the key between single or double quotes and inside brackets [ ]

dict_variable[“key1”] = value1

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

Length of a dictionary

A

len(dict_variable)

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

items( ) method on dictionary and when it cannot be used

A
  • Will return a list of tuples where the first element in each tuple is the key of the dictionary and the second element in each tuple is the value corresponding to that key
  • Cannot use list indexing with the items( ) method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

items( ) Syntax + output example

A

dict_variable.items( )

Output:
dict_variable( [ (‘key1’ , value1) , (‘key2’ , value2) , …] )

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

View Object - what it is and what it gives us

A
  • Is the information inside the dict_variable( [ ] ) in the output
  • Gives us a snapshot of what is in the dictionary (provides each key and respective value)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Get all Keys from Dictionary method and syntax

A

keys( )

dict_variable.keys( )

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

keys( ) method returns

A

Will return a view object that contains the keys of the dictionary as a list

dict_variable( [ ‘Key1’, ‘Key2’ , ‘Key3’ , …. ] )

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

Get all Values from Dictionary method and syntax

A

values( )

dict_varible.values( )

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

values( ) method returns

A

-Returns a view object that contains the values of the dictionary as a list

dict_variable( [‘value1’ , ‘value2’ , ‘value3’ …. ] )

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

2 ways to get a specific VALUE from a dictionary and a note about syntax

A

get( )

dict_variable[‘keyname’]

*both require the key to be in single or double quotes

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

get( ) syntax

A

dict_variable.get(‘keyname’)

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

Lists of Dictionaries

A

-Dictionaries can have the same keys associated with different values formatted as:
[ {‘key1’ : value1 , ‘key2’ : value2} , {‘key1’ : value3, ‘key2’ : value4} , ….]

-Each dictionary is wrapped in { }

17
Q

append( ) + dictionary w/ 2 keys syntax

A

dict_variable.append( {‘key1’ : value1 , ‘key2’ : value2})

18
Q

CSV Format and List of Dictionaries

A

Think of Keys as column headers and values as the respective row data

19
Q

List Methods (on dictionaries) can be used to (4x):

A
  1. Get the length of the dictionary - len(dict_variable)
  2. Use indexing and slicing to get one or more dictionaries - dict_variable[index]
  3. Use the append( ), insert( ), and remove( ) methods to add and remove one or more dictionaries
  4. Change a value for one of the keys in the list of dictionaries