Dictionary

NPTEL-NOC IITM
1 Sept 201912:27

Summary

TLDRThis lecture introduces Python dictionaries as a key-value pair data structure, using hash table concepts. It explains how to create, access, modify, and manipulate dictionaries with examples, including adding and updating key-value pairs, as well as removing elements using 'del' and 'clear' methods. The summary highlights the mutability of dictionaries and their practical applications in Python programming.

Takeaways

  • 📚 A dictionary in Python is a data structure that maps keys to values, forming key-value pairs.
  • 🔑 Dictionaries are enclosed by curly braces and use colons to separate keys and values, with keys on the left and values on the right.
  • 🚀 To create a dictionary, assign values to keys using the format: `dictionary_name = {key1: value1, key2: value2, ...}`.
  • 🖨 Printing a dictionary displays all key-value pairs contained within it.
  • 🔍 Accessing a specific value in a dictionary is done by referencing its key, using the format: `dictionary_name[key]`.
  • 🔑 The `keys()` method of a dictionary returns a list of all keys, and the `values()` method returns a list of all values.
  • 🔄 The `items()` method provides a list of key-value pairs in the form of tuples.
  • 🛠 Dictionaries are mutable, meaning you can add, modify, or remove key-value pairs.
  • 📝 To add a new key-value pair, assign a value to a new key within the dictionary, or use the `update()` method.
  • 🔄 To modify an existing key's value, assign a new value to the key within the dictionary.
  • 🗑 To remove a key-value pair, use the `del` statement followed by the dictionary name and the key to be removed.
  • 🧹 The `clear()` method removes all key-value pairs from a dictionary, resulting in an empty dictionary.

Q & A

  • What is a dictionary in Python?

    -A dictionary in Python is a data structure that works as a hash table. It maps keys to values and is composed of key-value pairs enclosed by curly braces.

  • How are keys and values represented in a Python dictionary?

    -In a Python dictionary, keys are placed to the left of the colon, and values are placed to the right. Keys are immutable types, usually numbers or strings.

  • Can you provide an example of creating a dictionary in Python?

    -Yes, an example of creating a dictionary is `Fuel_type = {'Petrol': 1, 'Diesel': 2, 'CNG': 3}`. Here, 'Petrol', 'Diesel', and 'CNG' are keys, and 1, 2, and 3 are their corresponding values.

  • How do you print a dictionary in Python?

    -You can print a dictionary in Python by using the `print` function followed by the dictionary name, e.g., `print(Fuel_type)`.

  • How can you access the value of a specific key in a dictionary?

    -To access the value of a specific key, you use the key name within square brackets after the dictionary name, like `Fuel_type['Petrol']` which would return the value 1.

  • What is the method to access all keys of a dictionary?

    -You can access all keys of a dictionary by using the `keys()` method, like `Fuel_type.keys()`, which returns a view of the dictionary's keys.

  • How do you access all values in a dictionary?

    -To access all values, you use the `values()` method, such as `Fuel_type.values()`, which returns a view of all the values in the dictionary.

  • Can you retrieve both keys and values at the same time from a dictionary?

    -Yes, you can retrieve both keys and values simultaneously using the `items()` method, e.g., `Fuel_type.items()`, which returns an iterable view of the dictionary's key-value pairs.

  • Is it possible to add a new key-value pair to an existing dictionary?

    -Yes, you can add a new key-value pair to an existing dictionary by directly assigning a value to a new key within the dictionary, like `Fuel_type['Electric'] = 4`.

  • What is the built-in method to update a dictionary with multiple new key-value pairs?

    -The `update()` method is the built-in function used to add multiple key-value pairs to a dictionary, e.g., `Fuel_type.update({'Electric': 4})`.

  • How can you modify the value of an existing key in a dictionary?

    -To modify the value of an existing key, you assign a new value to that key within the dictionary, like `Fuel_type['CNG'] = 5`.

  • What is the purpose of the `del` statement when working with dictionaries?

    -The `del` statement is used to remove a key-value pair from a dictionary, e.g., `del Fuel_type['Petrol']` which removes the 'Petrol' key and its associated value from the dictionary.

  • How do you clear all key-value pairs from a dictionary?

    -You can clear all key-value pairs from a dictionary by using the `clear()` method, like `Fuel_type.clear()`, which empties the dictionary.

  • What is the significance of dictionaries being mutable in Python?

    -The mutability of dictionaries in Python means that you can change their content after creation, allowing for the addition, modification, or removal of key-value pairs.

Outlines

00:00

📚 Introduction to Python Dictionaries

This paragraph introduces the concept of a dictionary in Python, a fundamental data structure that operates as a hash table and stores data in key-value pairs. Dictionaries are enclosed with curly braces and allow for the assignment of immutable keys to various data types as values. The lecture demonstrates the creation of a dictionary called 'Fuel_type' with keys such as 'Petrol', 'Diesel', and 'CNG', each associated with numerical values. It also covers how to print and access the dictionary's components using keys, as well as how to retrieve all keys and values using the 'keys()' and 'values()' methods, respectively. Additionally, the paragraph explains the 'items()' method, which returns a list of tuple pairs containing keys and their corresponding values.

05:02

🛠 Modifying Python Dictionaries

This section delves into the mutability of dictionaries, showing how to add, modify, and remove key-value pairs. It explains the process of adding a new key-value pair to an existing dictionary using direct assignment or the 'update()' method. The paragraph also illustrates how to change the value of an existing key by directly assigning a new value to it. Furthermore, it covers the use of the 'del' statement to remove a specific key-value pair and the 'clear()' method to empty the entire dictionary. The summary emphasizes the flexibility of dictionaries in Python for data manipulation.

10:05

🔍 Summary of Python Dictionary Operations

The final paragraph summarizes the operations covered in the lecture on Python dictionaries. It reiterates the creation of dictionaries using curly braces and colons to separate keys and values, with keys being of any immutable type and values being of any data type. The summary highlights the methods for accessing dictionary components through keys, adding elements with the 'update()' method or direct assignment, modifying existing key-values, and removing elements using 'del' or 'clear'. The paragraph concludes by emphasizing the dictionary's role as a versatile and mutable data structure in Python.

Mindmap

Keywords

💡Dictionary

A dictionary in Python is a mutable data structure that stores data in key-value pairs. It is analogous to a real-world dictionary where you can look up a word to find its meaning. In the context of the video, dictionaries are used to map keys to values, allowing for efficient data retrieval. For example, the script creates a dictionary called 'Fuel_type' with keys like 'Petrol', 'Diesel', and 'CNG', each associated with a numerical value.

💡Key-Value Pairs

Key-value pairs are the fundamental units of a dictionary, where a key is a unique identifier and the value is the data associated with that key. In the video script, the concept is exemplified by assigning values to fuel types, such as 'Petrol' being assigned the value '1', indicating a specific attribute or category.

💡Immutable

Immutable types in Python are data types whose values cannot be changed after they are created. In the script, it is mentioned that the keys in a dictionary are usually numbers or strings, both of which are immutable. This means once a key is set, it cannot be altered, ensuring the integrity of the data structure.

💡Mutable

Mutable data structures can be changed after they are created. Dictionaries in Python are mutable, as demonstrated in the script where new key-value pairs can be added, or existing values can be modified. This flexibility allows for dynamic data manipulation within a program.

💡Hash Table

A hash table is a data structure that implements an associative array, a structure that can map keys to values. In the script, dictionaries are described as an example of hash table data structures, which use a hash function to compute an index into an array of buckets or slots, from which the desired value can be found.

💡Access

Accessing components in a dictionary involves using the keys to retrieve the corresponding values. The script explains how to access values by using keys, such as accessing the value for 'Petrol' in the 'Fuel_type' dictionary, which would return the value '1'.

💡Update

The update method in Python dictionaries allows adding new key-value pairs or modifying existing ones. The script illustrates this by showing how to add a new key 'electric' with the value '4' to the 'Fuel_type' dictionary, expanding the data structure with new information.

💡Modify

Modification of a dictionary involves changing its content, such as updating the value of an existing key or adding new key-value pairs. The script demonstrates modifying the 'CNG' key's value from '3' to '5', showcasing how dictionaries can be dynamically altered.

💡Remove

Removing components from a dictionary is done using the 'del' statement or the 'clear' method. The script explains using 'del' to remove the 'Petrol' key-value pair from the 'Fuel_type' dictionary, which results in that entry no longer being part of the data structure.

💡Clear

The 'clear' method in Python dictionaries is used to remove all key-value pairs, effectively emptying the dictionary. In the script, it is mentioned as a way to reset the 'Fuel_type' dictionary to an empty state, with no key-value pairs remaining.

💡Items

The 'items' method in Python returns a view object that displays a list of a dictionary's key-value tuple pairs. The script refers to using 'items' to access both keys and values from the dictionary, such as displaying the pairs ('Petrol', 1), ('Diesel', 2), etc.

Highlights

A dictionary in Python is a data structure that maps keys to values, functioning as a hash table.

Dictionaries are created using curly braces and keys are separated from values with colons.

Keys in a dictionary can be numbers or strings and are immutable types.

Values in a dictionary can be of any data type.

The print function is used to display the contents of a dictionary.

Accessing components of a dictionary is done through keys, not index numbers.

The 'keys' method retrieves all the keys from a dictionary.

The 'values' method retrieves all the values from a dictionary.

The 'items' method returns key-value pairs from a dictionary.

Dictionaries are mutable, allowing for modification of their components.

New key-value pairs can be added to a dictionary using the assignment operator.

The 'update' method is a built-in function to add key-value pairs to a dictionary.

Existing keys in a dictionary can be assigned new values.

The 'del' function is used to remove key-value pairs from a dictionary.

The 'clear' method removes all key-value pairs, resulting in an empty dictionary.

A summary of creating, accessing, modifying, adding, and removing components from a Python dictionary.

Transcripts

play00:14

. Welcome to the lecture.

play00:16

In this lecture we will see what is mean by a Dictionary how to create a dictionary in

play00:23

python and also how to access the components of the dictionary, we will also see how to

play00:30

modify the components in the dictionary?

play00:32

First let us look at what does mean by dictionary?

play00:36

A dictionary is one of the data structure which is available in python so, it is an

play00:42

example of hash table data structures.

play00:46

Dictionary basically works like so, it maps keys to the values; so, it is basically called

play00:51

as a key value pairs.

play00:52

So, dictionaries are enclosed by curly braces.

play00:56

So, let us take an example and see what is meant by keys and what is meant by the values?

play01:00

So, let us look at these example, we have Petrol diesels; so, which as basically the

play01:07

Fuel types categories.

play01:08

So, I have some values in the right hand side 0s and 1s and 2.

play01:13

Let us say if I wanted to assign a value to the petrol, so these are called as basically

play01:19

the keys and these are called as a values.

play01:22

So, we can assign a value of 1 to the petrol; and you can also assign a value of 2 to the

play01:30

Diesel we will see how to create a dictionary.

play01:33

We will create a dictionary with different Fuel type categories.

play01:37

I will create a dictionary call Fuel underscore type with Petrol colon 1.

play01:44

So, this is basically the keys can be a usually numbers or a strings so, it is an immutable

play01:51

type.

play01:52

So, diesel, CNG these are the keys 1, 2, 3 are these are the values so; it can be of

play02:02

a any data type.

play02:03

So, to create a dictionary we will use a curly braces, apart from the curly braces we also

play02:10

use a colon to separate the keys and values.

play02:14

So, to the leftmost is of the colon is the keys, right most of the colon are call the

play02:22

values.

play02:23

We will see how to print the dictionary?

play02:26

So, to print the dictionary we have to use the command call print Fuel underscore type

play02:31

which is basically a dictionary name.

play02:34

The dictionary which we have created earlier will be displayed with the keys and values.

play02:39

So, Petrol will be assigned a value of 1; Diesel will be assigned a value of 2; CNG

play02:45

will be assigned a value of 3.

play02:47

Let us see how to access the components of the dictionary?

play02:50

So, we can access a components of a dictionary by using its keys.

play02:55

So, in this we saw using the based on the index number we access a components.

play03:01

But in dictionary we will be using the keys to access the components.

play03:06

So, let us say if I wanted to know the value of the key, Petrol from the dictionary Fuel

play03:14

underscore type.

play03:15

So, I will use this below command print; Fuel underscore type which is basically have a

play03:21

dictionary name.

play03:22

So, I wanted to know the value of the key petrol.

play03:25

So, within the square bracket so, I will mention the key name so which is the petrol.

play03:31

Once you have done.

play03:32

So, I will be getting an output of 1; similarly to access the remaining values so, you can

play03:37

use this format.

play03:38

Next I wanted to access the keys from the dictionary Fuel underscore type.

play03:44

Syntax: is dictionary underscore name which is our Fuel underscore type dictionary name

play03:52

dot keys.

play03:53

If you give Fuel underscore type which is our basically dictionary name dot keys.

play03:58

So, it throws you on output of keys.

play04:02

So, it shows as dict underscore keys, which has basically values Petrol, Diesel and CNG.

play04:11

If you wanted to access the values from the dictionary Fuel underscore type the Syntax

play04:17

is again dictionary name.

play04:20

So, we have to replace the keys with values now because, we need to access the values

play04:26

from the dictionary; so, dot values.

play04:29

So, if you give Fuel underscore type dot values.

play04:33

So, it basically throws you an values.

play04:35

So, now, dict underscore values of 1, 2 and 3.

play04:38

So, 1 corresponds to petrol, 2 corresponds to Diesel and three corresponds to the CNG.

play04:48

Next if you wanted to access both the keys and values, there is one in built command

play04:55

which gives as an output Syntaxes dictionary underscore name dot items.

play05:02

So, which is basically dictionary name is Fuel underscore type dot items it throws you

play05:09

a key value pairs which is basically keys and values.

play05:14

So, we have dict items Petrol which is our key and one corresponds to the value of that

play05:20

key.

play05:21

Similarly we have a Diesel and the corresponding value CNG and the corresponding values it

play05:28

displays a output with the less format of.

play05:32

It returns an elements in the list format with the key and value which is a tuple pairs.

play05:38

So, these are the tuple pairs so, Petrol 1.

play05:40

So, they have been enclose in a parentheses so, this becomes our tuple.

play05:44

So, this is our another tuple this is our another tuple so it has a tuple pairs.

play05:49

Dictionary is a mutable data structure we can also modify the components in the dictionary,

play05:56

let us see how to do that.

play05:58

So, we can also add a new key value pair to the existing dictionary; Fuel underscore type

play06:05

using the keys.

play06:06

So, basically the Syntax is dictionary underscore name the new key which has to be added to

play06:15

the existing dictionary and the new value which has to be given to the key respective

play06:20

key.

play06:21

So, now, I wanted to add the fourth key value path to the existing dictionary or dictionary

play06:27

name is Fuel underscore type.

play06:30

So, I wanted to add a new key which is a electric.

play06:34

So, it has a value of 4.

play06:36

So, when you print the updated dictionary when you use a print command Fuel underscore

play06:42

type it will be throwing an output which says four key value pairs.

play06:47

So, which is basically Petrol Diesel 2 CNG 3 and now electric four which has been added

play06:55

to the existing dictionary.

play06:56

So, this is we have done using the key value pairs.

play07:00

So, I am assigning a key and value to the existing dictionary.

play07:03

So, you can also use the built in functions which is available in python we will also

play07:08

look at how to do that.

play07:09

So, the command is using the update command you can also add the key value pairs to the

play07:15

existing dictionary.

play07:16

So, the Syntax is we have to specify the dictionary name dot update within the parentheses you

play07:26

have to specify the key, which you wanted to add and the colon and the corresponding

play07:32

values.

play07:33

So, the dictionary name is Fuel underscore type I am using the built in function which

play07:40

is update so, dot update key which is a electric.

play07:45

So, I am using the single quotes colon four which is the corresponding value for the electric,

play07:52

when you print the updated dictionary you will be getting an output like this.

play07:56

So, we can also assign a key values to the existing dictionary or else can you see update

play08:02

function as well.

play08:04

So, if you wanted to modify the value of an existing key in the Fuel underscore type,

play08:10

we can also do that.

play08:11

Let us see how to do that so, assign a value to be changed to the corresponding key of

play08:17

the dictionary.

play08:18

So, basically our dictionary is Fuel underscore type right.

play08:22

So, this is our existing dictionary I have four key value pairs, which is Petrol is corresponds

play08:28

to 1, Diesel corresponds to 2, CNG corresponds to 3.

play08:32

So, now, we wanted to change the value of 3 to 5 right.

play08:38

So, we will look at how to do that?

play08:41

So, dictionary name which is our existing dictionary Fuel underscore type.

play08:46

So, we wanted to update the value of 3 to 5 right.

play08:50

So, within the square brackets mention the key which is CNG and the corresponding value

play08:57

which you wanted to update now.

play08:59

So, now, I am specifying 5.

play09:00

When you print updated dictionary you will be able to see the changes.

play09:07

So, now, CNG value which has 3; now it has been change to 5.

play09:13

So, we can also modify the value of the existing key as well.

play09:17

We saw how to add components to the dictionary; we will also see how to remove components

play09:23

from the dictionary as well?

play09:24

So, there are built in functions so, you can use the del function as well.

play09:30

So, del what it does is it removes the key value pairs from the dictionary.

play09:37

Syntax is del followed by the dictionary name and the respective key which you wanted to

play09:43

remove.

play09:44

So, let us say if you wanted to remove Petrol from the Fuel underscore type, the command

play09:51

will be del Fuel underscore type which is basically have a dictionary name so, the key

play09:58

is Petrol right.

play10:00

So, when use that so, del Fuel underscore type and Petrol.

play10:05

So, if you print the updated dictionary we already had four key value pairs.

play10:11

So, once we have use this command so, it the Petrol and the corresponding value has been

play10:18

deleted from the dictionary.

play10:19

So, now, you will have only three key value pairs which is basically, Diesel which corresponds

play10:25

to 2, CNG we already modified 3 to 5.

play10:29

So, now, CNG will have a value of 5 and electric which has a value of 4.

play10:35

Next we look at how to clear the key value pairs from the dictionary?

play10:40

So, clear what it does is it removes all the key value pairs from the dictionary and it

play10:48

will be a null dictionary.

play10:49

So, the Syntax is dictionary name dot clear function.

play10:55

So, if you use Fuel underscore type dot clear, when you print the updated dictionary.

play11:02

So, it will written a null dictionary.

play11:05

So, there will not be any elements in this dictionary.

play11:09

So, let us summarise we saw how to create a dictionary.

play11:13

So, the dictionaries were created using the curly brackets apart from the curly brackets.

play11:18

So, there was also a colon.

play11:20

So, to the left of the colon is called as a key, to the right of the colon is called

play11:26

as a values.

play11:27

So, the keys are mutable, values can be of a any data type.

play11:32

So, we also saw how to access the components.

play11:35

So, you can access the components using the keys from the dictionary.

play11:39

We also saw how to modify the dictionary we also saw how to add a elements to the dictionary

play11:45

using a update.

play11:46

And also we saw how to assign a key values to the existing dictionary and we also looked

play11:52

at how to remove elements from the dictionary.

play11:55

So, you can use one is del command so, which removes a key value pairs from the dictionary

play12:02

or else you can use the clear command as well.

play12:05

Thank you .

Rate This

5.0 / 5 (0 votes)

Related Tags
PythonDictionariesData StructuresKey-Value PairsHash TablesMutableImmutabilityProgrammingTutorialEducational