Python - Dictionaries

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

How do you get information from a dictionary?

By specifying a key in brackets you can access the value associated with that key. >>> adict {'a': 1, 'b': 2, 'c': 3} >>> adict['a'] 1

What does the dictionary method keys() do?

Keys() returns all the possible keys within the dictionary as a dict_keys object. >>> adict.keys() dict_keys(['a', 'b', 'c'])

How can you add key/values to a dictionary?

You can create a new key and value using brackets and the assignment operator. >>> adict {'a': 1, 'b': 2, 'c': 3} >>> adict['d'] = 4 >>> adict {'a': 1, 'b': 2, 'c': 3, 'd': 4}

How do you work with a list that is stored in a dictionary?

>>> for sport, types in sports.items(): ... print(f'{sport}: {str(types)}') ... summer: ['running', 'swimming'] winter: ['skiing', 'ice skating']

What does the dictionary method get() do?

get(<key>) will get the value associated with the specified key. If it exists. >>> adict {'a': 1, 'b': 2, 'c': 3} >>> adict.get('a') 1

What is a dictionary?

A data type of key: value pairs. With the requirement that keys are unique. A key can be a string, integer, tuple The values can be any element. Each key : value is separated by a colon. Each element in the dictionary is separated by a comma. A pair of braces creates an empty dictionary: adict = {} >>> adict = {'a':1, 'b':2, 'c':3} >>> adict {'a': 1, 'b': 2, 'c': 3}

How do you loop through all the key-value pairs in a dictionary?

Using a for loop and items() method. >>> for key, value in adict.items(): ... print(key + str(value)) a1 b2 c3

How do you loop through all the keys in a dictionary?

Using a for loop and keys() method. >>> for key in adict.keys(): ... print(key) a b c

How do you loop through all the values in a dictionary?

Using a for loop and values() method. >>> for value in adict.values(): ... print(value) 1 2 3

How do you work with dictionaries stored in a list?

Using a for loop, each iteration will be assigned the next dictionary in the list. >>> for person in people: ... print(f"Name: {person['name']}, Age: {person['age']}") ... Name: John, Age: 30

How do you store a set of dictionaries in a list?

Using append() you can append a dictionary element to a list. >>> people = [] >>> people.append(person_dict) >>> people [{'name': 'John', 'age': '30'}]

How do you store a list in a dictionary?

Values in a dictionary can be a list element. >>> sports {'summer': ['running', 'swimming'], 'winter': ['skiing', 'ice skating']}

How can you remove a key/value from a dictionary?

del and specify the dictionary[key] >>> adict {'a': 1, 'b': 2, 'c': 3, 'd': 4} >>> del adict['d'] >>> adict {'a': 1, 'b': 2, 'c': 3}

What does the dictionary method items() do?

items() returns all the key/values stored in the dictionary as a dict_items object. >>> adict.items() dict_items([('a', 1), ('b', 2), ('c', 3)])

What does the dictionary method values() do?

values() returns all the possible values within the dictionary as a dict_values object. >>> adict.values() dict_values([1, 2, 3])


Kaugnay na mga set ng pag-aaral

Kinesiology Mid-term (True/False)

View Set

WEEK 4 - DIFFUSION AND ADOPTION PROCESS

View Set

Evolution & Environmental Change

View Set

Digestive System (pancreas, liver, gallbladder)

View Set

Med Surg - Chapter 24 - Assessment of the Skin, Hair, and Nails

View Set