Python Dictionaries

¡Supera tus tareas y exámenes ahora con Quizwiz!

Accessing dictionary items

Dictionary items can be accessed by referring to an item's key name, inside square brackets

Dictionary method clear()

Removes all the elements from the dictionary

Removing items from a dictionary using the popitem() method thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964, "color": "red" }

popitem() removes the last inserted item thisdict["model"] = "Mustang" print(thisdict) >>>{'brand': 'Ford', 'year': 2018, 'color': 'red', 'model': 'Mustang'} thisdict.popitem() >>>('model', 'Mustang') print(thisdict) >>>{'brand': 'Ford', 'year': 2018, 'color': 'red'}

Determine how many items (key-value pairs) a dictionary has thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 }

print(len(thisdict))

Print the value of the "model" key

print(thisdict["model"]) >>>Mustang

Removing items from a dictionary using the pop() method thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964, "color": "red" }

the pop() method removes the item with the specified key name thisdict.pop("model") >>>'Mustang' print(thisdict) >>>{'brand': 'Ford', 'year': 2018, 'color': 'red'}

Create and print a dictionary using the dict() constructor

thisdict = dict(brand="Ford", model="Mustang", year=1964) #note that keywords are not string literals #note the use of equals rather than colon for the assignment

Create and print a dictionary

thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } print(thisdict) >>>{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}

Add an item to the dictionary thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 }

thisdict["color"] = "red" ***Use a new index key and assign a value to it print(thisdict) >>>{'brand': 'Ford', 'model': 'Mustang', 'year': 2018, 'color': 'red'}

Change the "year to 2018 thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 }

thisdict["year"] = 2018

Get the value of the "model" key using the get() method

x = thisdict.get("model") print(x) >>>Mustang

Get the value of the "model" key

x = thisdict["model"] print(x) >>>Mustang

Dictionary method pop()

Removes the element with the specified key

Dictionary method popitem()

Removes the last inserted key-value pair

Dictionary method copy()

Returns a copy of the dictionary

Dictionary method fromkeys()

Returns a dictionary with the specified keys and values

Check if KEY exists

To determine if a specified key is present in a dictionary use the in keyword

Dictionary method update()

Updates the dictionary with the specified key-value pairs

Change dictionary values

You can change the value of a specific item by referring to its key name

Loop through both KEYS AND VALUES using the items() function thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 }

for x, y in thisdict.items(): print(x, y) >>>brand Ford >>>model Mustang >>>year 2018

Check if "model" is present in the dictionary thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 }

if "model" in thisdict: print("Yes, 'model' is one of the keys in the thisdict dictionary")

Copy the dictionary using the dict() method thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 }

mydict = dict(thisdict)

Copy the dictionary using the copy() method thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 }

mydict = thisdict.copy()

Dictionary

A dictionary is a collection which is unordered, changeable, and indexed. In Python, dictionaries are written with curly brackets, and they have keys and values.

Dictionary method items()

Returns a list containing the a tuple for each key value pair

Dictionary method keys()

Returns a list containing the dictionary's keys

Dictionary method values()

Returns a list of all the values in the dictionary

Dictionary method get()

Returns the value of the specified key

Dictionary method setdefault()

Returns the value of the specified key. If the key does not exist: insert the key, with the specified value

Loop through a dictionary

You can loop through a dictionary by using a for loop ***When looping through a dictionary, the return values are the KEYS of the dictionary ***There are separate methods for returning the VALUES

Copying a dictionary

You cannot copy a dictionary simply by typing dict2 = dict1, because dict2 will only be a reference to dict1. Changes made in dict1 will automatically also be made in dict2.

Removing items from a dictionary with the clear() keyword thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 }

clear() empties the dictionary of all key-value pairs thisdict.clear() print(thisdict) >>>{}

Deleting a dictionary thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 }

del can also delete the dictionary completely del thisdict

Removing items from a dictionary using the del keyword thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964, "color": "red" }

del removes the item with the specified key name del thisdict["year"] print(thisdict) >>>{'brand': 'Ford', 'color': 'red'}

Get the values of the dictionary using the values() function thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 }

for x in thisdict.values(): print(x) >>>Ford >>>Mustang >>>2018

Print all key names in the dictionary one-by-one thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 }

for x in thisdict: print x >>>brand >>>model >>>year

Print all values in the dictionary one-by-one thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 }

for x in thisdict: print(thisdict[x]) >>>Ford >>>Mustang >>>2018


Conjuntos de estudio relacionados

Financial Accounting - Chapter 4 - Self Study Questions

View Set

Saunders- Ch.49: Gastrointestinal Medications

View Set

QUINTO GRADO, ¿De qué color es la bandera?

View Set

Propositional Logic Definitions (Ch. 7)

View Set

Español 322 - Capítulo 10 - Posición de los Adjetivos

View Set

PrepU 39: Management of Patients With Oral and Esophageal Disorders

View Set

Chapter 1 Quiz - Introduction to Marketing - Vincennes University - Professor Murphy's Course

View Set