Logo

Dictionaries

Understanding dictionaries allows you to better properly model a range of real-world items. One will be able to design a dictionary that represents a person and then store as much information about that person as you want in it.

You can characterize a person, like save information like name, age, location, occupation, and any other information you like.

You'll be able to store any two types of information that can be matched, such as a list of words and their definitions, or a list of numbers and their meanings.

A list of people's names and favorite numbers, a list of mountains, and a list of numbers..etc
.

 

Create a simple Dictionary

In Python, a dictionary is a collection of key-value pairs.

A key-value pair is a collection of values that are linked to one another.

Individual key-value pairs are separated by commas, and each key is connected to its value by a colon. eg. (',')

 

 

Accessing values in dictionary

Each key corresponds to a value, and you can access the value associated with that key by using that key.

 

 

 

Add new key-value pair

A number, a text, a list, or even another dictionary can be used as the value of a key. In fact, every object created in Python can be used as a value in a dictionary.

 

 

 

Looping through the key-value pairs of Dictionary

 

 

Loop through all the Keys

 

 

Looping through all the Values

 

A dictionary is enclosed in braces, with a set of keyvalue pairs contained within the braces.

Python returns the value associated with a key when you give one.

In a dictionary, you can store as many key-value pairs as you wish.

When you no longer need a piece of information that’s stored in a dictionary, you can use the del statement to completely remove a key-value pair.

 

Deleting a value in Dictionary

 

Starting with an empty dictionary and adding each new entry to it is sometimes useful, if not required.

To begin filling an empty dictionary, create an empty set of braces and then add each key-value pair on its own line.

 

Create an empty dictionary

 

To change a value in a dictionary, type the dictionary's name followed by the key in square brackets and the new value you want to be associated with that key.

 

Modifying values in a dictionary

 

Using sorted() in a dictionary.. prints in order

The title() function capitalises the first letter of a string. 

 

 

When you use set() on a list with duplicate items, Python looks for the unique elements in the list and creates a set out of them.

In favorite languages.values, we utilize set() to extract the unique languages ().

 

 

Sometimes you'll want to save a list of dictionaries or a list of things as a dictionary value.

This is referred to as nesting.

A list of dictionaries, a list of objects inside a dictionary, or even a dictionary inside another dictionary can all be nestled.

As the following examples indicate, nesting is a powerful feature.

 

List of dictionaries

The car 0 dictionary has enough room to store information about one vehicle, but not enough to store information about another.

How do you deal with a swarm of automobiles? Making a list of automobiles in which each car is a dictionary of information about that car is one method.

The following code, for example, generates a list of three cars:

 

 

With code that produces each car on its own We'll use range() to make a fleet of 30 cars in the following example :

 

 

Modifying the 1st 3 inserts of dictionary

We loop over a slice that only includes the first three car since we only want to change the first three cars.

A car

Figure 9.1. An illustration.

 

All of the cars are currently have a mileage of  '12.5', but this won't always be the case, so we construct an if statement to ensure that we're only updating the 1st 3 cars when we modify the dictionaries.

If the car is has a mileage of '12.5', we change the color,mileage and speed  to 'yellow','18.32', 'IDK.. where am i ?' respectively.

The same has been done below :

 

List in a dictionary

It's sometimes better to place a list inside a dictionary.

Example describe a cake that someone has placed an order for.

 

A cake

Figure 9.2. An illustration.

 

If you merely used a list, all you'd be able to save is a list of cake types.

A list of consistency can be simply one component of the cake you're describing with a dictionary.

For each cake in the following example, two types of data are stored: the 'flavor' and a list of 'consistency'.

 

 

Dictionary in a dictionary

Although you can nest a dictionary inside another dictionary, this can quickly confuse your code.

For example, if a website has multiple users, each with their own username, the usernames can be used as keys in a dictionary.

You can then use a dictionary as the value linked with each user's username to keep information about them.

We maintain three pieces of information about each user in the following list: their first name, last name, and location.

We'll get this data by cycling through the usernames and the information dictionaries linked with each one :

 

 

The main characteristic of a dictionary is that it stores arbitrary indexed unordered  data type.
because they don’t keep track of the order of their elements. When you request to see the contents of the dictionary, you may or may not get the elements in the same order as they were entered:
Query Dictionary Values

Use get to retrieve a value from a dictionary without risking an exception we can use get(k,x).

K is the key of the element to extract, and x is the element that will be returned if k isn't found in the dictionary as a key.






Below table shows the set of operations which we can perform on dictionaries :

Properties Description
len(dictionary)
Number of elements of d
dictionary[key]
The element from d that has a k key
dictionary[key]=value
Set d[k] to v
del dictionary[key]
Remove d[k] from d
dictionary.clear()
Remove all items from d
dictionary.copy()
Copy d
key in dictionary
True if d has a key k, else False
key not in dictionary
Equivalent to not k in d
dictionary.has_key(key)
Equivalent to k in d, use that form in new
code
dictionary.items()
A copy of d’s list of (key, value) pairs
dictionary.keys()
A copy of d’s list of keys
dictionary.values()
A copy of d’s list of values
dictionary.update([b])
Updates (and overwrites) key/value pairs
from b
d.fromkeys(seq[value])
A copy of d’s list of values</di
d.get(k[, x])
a[k] if k in d, else x
d.setdefault(k[, x])
a[k] if k in d, else x (also setting it)
d.pop(k[, x])
d[k] if k in d, else x (and remove k)
d.popitem()
Remove and return an arbitrary (key, value)
pair


If you don't provide x and the dictionary doesn't have a k key, the method returns None.

Using dictionary as a dictionary comprehension :





---- Summary ----

As of now you know dictionary & how dictionaries work.

  • Create a simple Dictionary

  • Accessing values in dictionary

  • Add new key-value pair

  • Looping through the key-value pairs of Dictionary

  • Looping through keys

  • Looping through values

  • Delete values of a dictionary

  • Create empty dictionary

  • Modify values of dictionary

  • List in a dictionary

  • dictionary in dictionary

  • ... & much more


________________________________________________________________________________________________________________________________
Footer
________________________________________________________________________________________________________________________________

Copyright © 2022-2023. Anoop Johny. All Rights Reserved.