0% found this document useful (0 votes)
23 views

Dictionaries in Python Day 1

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Dictionaries in Python Day 1

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Dictionaries in Python

Dictionaries are unordered collections of unique values stored in (Key-Value) pairs.

Python dictionary represents a mapping between a key and a value. In simple terms, a Python
dictionary can store pairs of keys and values. Each key is linked to a specific value. Once stored in a
dictionary, you can later obtain the value using just the key.

Characteristics of dictionaries

 Unordered: The items in dictionaries are stored without any index value, which is typically a
range of numbers. They are stored as Key-Value pairs, and the keys are their index, which will not
be in any sequence.
 Unique: As mentioned above, each value has a Key; the Keys in Dictionaries should be unique. If
we store any value with a Key that already exists, then the most recent value will replace the old
value.
 Mutable: The dictionaries are collections that are changeable, which implies that we can add or
remove items after the creation.
AD

Creating a dictionary
There are following three ways to create a dictionary.

 Using curly brackets: The dictionaries are created by enclosing the comma-separated Key:
Value pairs inside the {} curly brackets. The colon ‘: ‘ is used to separate the key and value in a
pair.
 Using dict() constructor: Create a dictionary by passing the comma-separated key: value
pairs inside the dict().
 Using sequence having each item as a pair (key-value)
AD

Let’s see each one of them with an example.

Example:

# create a dictionary using {}


person = {"name": "Jessa", "country": "USA", "telephone": 1178}
print(person)
# output {'name': 'Jessa', 'country': 'USA', 'telephone': 1178}

# create a dictionary using dict()


person = dict({"name": "Jessa", "country": "USA", "telephone": 1178})
print(person)
# output {'name': 'Jessa', 'country': 'USA', 'telephone': 1178}

# create a dictionary from sequence having each item as a pair


person = dict([("name", "Mark"), ("country", "USA"), ("telephone", 1178)])
print(person)

# create dictionary with mixed keys keys


# first key is string and second is an integer
sample_dict = {"name": "Jessa", 10: "Mobile"}
print(sample_dict)
# output {'name': 'Jessa', 10: 'Mobile'}

# create dictionary with value as a list


person = {"name": "Jessa", "telephones": [1178, 2563, 4569]}
print(person)
# output {'name': 'Jessa', 'telephones': [1178, 2563, 4569]}

Run
AD

Empty Dictionary
When we create a dictionary without any elements inside the curly brackets then it will be an empty
dictionary.

emptydict = {}
print(type(emptydict))
# Output class 'dict'

Run

Note:

 A dictionary value can be of any type, and duplicates are allowed in that.
 Keys in the dictionary must be unique and of immutable types like string, numbers, or tuples.
AD

Accessing elements of a dictionary


There are two different ways to access the elements of a dictionary.

1. Retrieve value using the key name inside the [] square brackets
2. Retrieve value by passing key name as a parameter to the get() method of a dictionary.

Example

# create a dictionary named person


person = {"name": "Jessa", "country": "USA", "telephone": 1178}

# access value using key name in []


print(person['name'])
# Output 'Jessa'

# get key value using key name in get()


print(person.get('telephone'))
# Output 1178

Run
AD
As we can see in the output, we retrieved the value ‘Jessa’ using key ‘name” and value 1178 using its
Key ‘telephone’.

Get all keys and values


Use the following dictionary methods to retrieve all key and values at once

Method Description

keys() Returns the list of all keys present in the dictionary.

values(
Returns the list of all values present in the dictionary
)

Returns all the items present in the dictionary. Each item will be
items()
inside a tuple as a key-value pair.

AD

We can assign each method’s output to a separate variable and use that for further computations if
required.

Example

person = {"name": "Jessa", "country": "USA", "telephone": 1178}

# Get all keys


print(person.keys())
# output dict_keys(['name', 'country', 'telephone'])
print(type(person.keys()))
# Output class 'dict_keys'

# Get all values


print(person.values())
# output dict_values(['Jessa', 'USA', 1178])
print(type(person.values()))
# Output class 'dict_values'

# Get all key-value pair


print(person.items())
# output dict_items([('name', 'Jessa'), ('country', 'USA'), ('telephone',
1178)])
print(type(person.items()))
# Output class 'dict_items'

You might also like