Python Dictionaries

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

Python dictionaries

A python dictionary is an unordered collection of items. It contains data as a set of key: value pairs.

Merging Dictionaries with the .update() Method in Python

Given two dictionaries that need to be combined, Python makes this easy with the .update() function. For dict1.update(dict2), the key-value pairs of dict2 will be written into the dict1 dictionary. For keys in both dict1 and dict2, the value in dict1 will be overwritten by the corresponding value in dict2.

Dictionary value types

Python allows the values in a dictionary to be any type - string, integer, a list, another dictionary, boolean, etc. However, keys must always be an immutable data type, such as strings, numbers, or tuples. In the example code block, you can see that the keys are strings or numbers (int or float). The values, on the other hand, are many varied data types.

The .pop() Method for Dictionaries in Python

Python dictionaries can remove key-value pairs with the .pop() method. The method takes a key as an argument and removes it from the dictionary. At the same time, it also returns the value that it removes from the dictionary.

get() Method for Dictionary

Python provides a .get() method to access a dictionary value if it exists. This method takes the key as the first argument and an optional default value as the second argument, and it returns the value for the specified key if key is in the dictionary. If the second argument is not specified and key is not found then None is returned.

Syntax of the Python dictionary

The syntax for a Python dictionary begins with the left curly brace ({), ends with the right curly brace (}), and contains zero or more key : value items separated by commas (,). The key is separated from the value by a colon (:).

Accessing and writing data in a Python dictionary

Values in a Python dictionary can be accessed by placing the key within square brackets next to the dictionary. Values can be written by placing key within square brackets next to the dictionary and using the assignment operator (=). If the key already exists, the old value will be overwritten. Attempting to access a value with a key that does not exist will cause a KeyError.

Dictionary accession methods

When trying to look at the information in a Python dictionary, there are multiple methods that access the dictionary and return lists of its contents. .keys() returns the keys (the first object in the key-value pair), .values() returns the values (the second object in the key-value pair), and .items() returns both the keys and the values as a tuple.


Kaugnay na mga set ng pag-aaral

Finding Distance in the Coordinate Plane /Instruction/ assignment

View Set

Fluid and Electrolyte Review - PC Nursing

View Set

Chapter 03: Hereditary Influences on Health Promotion of the Child and Family

View Set

ECON - Ch14.3 - Health, Disability, and Life Insurance - Section 3 - Disability Insurance

View Set

Basecamp: Reliability and Validity

View Set

Ch 38: Assessment and Management of Patients with Rheumatic Disease (1)

View Set