Dictionary
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
📚 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.
🛠 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.
🔍 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
💡Key-Value Pairs
💡Immutable
💡Mutable
💡Hash Table
💡Access
💡Update
💡Modify
💡Remove
💡Clear
💡Items
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
. Welcome to the lecture.
In this lecture we will see what is mean by a Dictionary how to create a dictionary in
python and also how to access the components of the dictionary, we will also see how to
modify the components in the dictionary?
First let us look at what does mean by dictionary?
A dictionary is one of the data structure which is available in python so, it is an
example of hash table data structures.
Dictionary basically works like so, it maps keys to the values; so, it is basically called
as a key value pairs.
So, dictionaries are enclosed by curly braces.
So, let us take an example and see what is meant by keys and what is meant by the values?
So, let us look at these example, we have Petrol diesels; so, which as basically the
Fuel types categories.
So, I have some values in the right hand side 0s and 1s and 2.
Let us say if I wanted to assign a value to the petrol, so these are called as basically
the keys and these are called as a values.
So, we can assign a value of 1 to the petrol; and you can also assign a value of 2 to the
Diesel we will see how to create a dictionary.
We will create a dictionary with different Fuel type categories.
I will create a dictionary call Fuel underscore type with Petrol colon 1.
So, this is basically the keys can be a usually numbers or a strings so, it is an immutable
type.
So, diesel, CNG these are the keys 1, 2, 3 are these are the values so; it can be of
a any data type.
So, to create a dictionary we will use a curly braces, apart from the curly braces we also
use a colon to separate the keys and values.
So, to the leftmost is of the colon is the keys, right most of the colon are call the
values.
We will see how to print the dictionary?
So, to print the dictionary we have to use the command call print Fuel underscore type
which is basically a dictionary name.
The dictionary which we have created earlier will be displayed with the keys and values.
So, Petrol will be assigned a value of 1; Diesel will be assigned a value of 2; CNG
will be assigned a value of 3.
Let us see how to access the components of the dictionary?
So, we can access a components of a dictionary by using its keys.
So, in this we saw using the based on the index number we access a components.
But in dictionary we will be using the keys to access the components.
So, let us say if I wanted to know the value of the key, Petrol from the dictionary Fuel
underscore type.
So, I will use this below command print; Fuel underscore type which is basically have a
dictionary name.
So, I wanted to know the value of the key petrol.
So, within the square bracket so, I will mention the key name so which is the petrol.
Once you have done.
So, I will be getting an output of 1; similarly to access the remaining values so, you can
use this format.
Next I wanted to access the keys from the dictionary Fuel underscore type.
Syntax: is dictionary underscore name which is our Fuel underscore type dictionary name
dot keys.
If you give Fuel underscore type which is our basically dictionary name dot keys.
So, it throws you on output of keys.
So, it shows as dict underscore keys, which has basically values Petrol, Diesel and CNG.
If you wanted to access the values from the dictionary Fuel underscore type the Syntax
is again dictionary name.
So, we have to replace the keys with values now because, we need to access the values
from the dictionary; so, dot values.
So, if you give Fuel underscore type dot values.
So, it basically throws you an values.
So, now, dict underscore values of 1, 2 and 3.
So, 1 corresponds to petrol, 2 corresponds to Diesel and three corresponds to the CNG.
Next if you wanted to access both the keys and values, there is one in built command
which gives as an output Syntaxes dictionary underscore name dot items.
So, which is basically dictionary name is Fuel underscore type dot items it throws you
a key value pairs which is basically keys and values.
So, we have dict items Petrol which is our key and one corresponds to the value of that
key.
Similarly we have a Diesel and the corresponding value CNG and the corresponding values it
displays a output with the less format of.
It returns an elements in the list format with the key and value which is a tuple pairs.
So, these are the tuple pairs so, Petrol 1.
So, they have been enclose in a parentheses so, this becomes our tuple.
So, this is our another tuple this is our another tuple so it has a tuple pairs.
Dictionary is a mutable data structure we can also modify the components in the dictionary,
let us see how to do that.
So, we can also add a new key value pair to the existing dictionary; Fuel underscore type
using the keys.
So, basically the Syntax is dictionary underscore name the new key which has to be added to
the existing dictionary and the new value which has to be given to the key respective
key.
So, now, I wanted to add the fourth key value path to the existing dictionary or dictionary
name is Fuel underscore type.
So, I wanted to add a new key which is a electric.
So, it has a value of 4.
So, when you print the updated dictionary when you use a print command Fuel underscore
type it will be throwing an output which says four key value pairs.
So, which is basically Petrol Diesel 2 CNG 3 and now electric four which has been added
to the existing dictionary.
So, this is we have done using the key value pairs.
So, I am assigning a key and value to the existing dictionary.
So, you can also use the built in functions which is available in python we will also
look at how to do that.
So, the command is using the update command you can also add the key value pairs to the
existing dictionary.
So, the Syntax is we have to specify the dictionary name dot update within the parentheses you
have to specify the key, which you wanted to add and the colon and the corresponding
values.
So, the dictionary name is Fuel underscore type I am using the built in function which
is update so, dot update key which is a electric.
So, I am using the single quotes colon four which is the corresponding value for the electric,
when you print the updated dictionary you will be getting an output like this.
So, we can also assign a key values to the existing dictionary or else can you see update
function as well.
So, if you wanted to modify the value of an existing key in the Fuel underscore type,
we can also do that.
Let us see how to do that so, assign a value to be changed to the corresponding key of
the dictionary.
So, basically our dictionary is Fuel underscore type right.
So, this is our existing dictionary I have four key value pairs, which is Petrol is corresponds
to 1, Diesel corresponds to 2, CNG corresponds to 3.
So, now, we wanted to change the value of 3 to 5 right.
So, we will look at how to do that?
So, dictionary name which is our existing dictionary Fuel underscore type.
So, we wanted to update the value of 3 to 5 right.
So, within the square brackets mention the key which is CNG and the corresponding value
which you wanted to update now.
So, now, I am specifying 5.
When you print updated dictionary you will be able to see the changes.
So, now, CNG value which has 3; now it has been change to 5.
So, we can also modify the value of the existing key as well.
We saw how to add components to the dictionary; we will also see how to remove components
from the dictionary as well?
So, there are built in functions so, you can use the del function as well.
So, del what it does is it removes the key value pairs from the dictionary.
Syntax is del followed by the dictionary name and the respective key which you wanted to
remove.
So, let us say if you wanted to remove Petrol from the Fuel underscore type, the command
will be del Fuel underscore type which is basically have a dictionary name so, the key
is Petrol right.
So, when use that so, del Fuel underscore type and Petrol.
So, if you print the updated dictionary we already had four key value pairs.
So, once we have use this command so, it the Petrol and the corresponding value has been
deleted from the dictionary.
So, now, you will have only three key value pairs which is basically, Diesel which corresponds
to 2, CNG we already modified 3 to 5.
So, now, CNG will have a value of 5 and electric which has a value of 4.
Next we look at how to clear the key value pairs from the dictionary?
So, clear what it does is it removes all the key value pairs from the dictionary and it
will be a null dictionary.
So, the Syntax is dictionary name dot clear function.
So, if you use Fuel underscore type dot clear, when you print the updated dictionary.
So, it will written a null dictionary.
So, there will not be any elements in this dictionary.
So, let us summarise we saw how to create a dictionary.
So, the dictionaries were created using the curly brackets apart from the curly brackets.
So, there was also a colon.
So, to the left of the colon is called as a key, to the right of the colon is called
as a values.
So, the keys are mutable, values can be of a any data type.
So, we also saw how to access the components.
So, you can access the components using the keys from the dictionary.
We also saw how to modify the dictionary we also saw how to add a elements to the dictionary
using a update.
And also we saw how to assign a key values to the existing dictionary and we also looked
at how to remove elements from the dictionary.
So, you can use one is del command so, which removes a key value pairs from the dictionary
or else you can use the clear command as well.
Thank you .
Ver Más Videos Relacionados
Dictionaries | Godot GDScript Tutorial | Ep 12
Sequence_data_part_2
DATA TYPES in Python (Numbers, Strings, Lists, Dictionary, Tuples, Sets) - Python for Beginners
AQA A’Level Vectors - Part 1, Overview & representation
Data Types in Python | Python for Beginners
Coding Exercise for Beginners in Python with solution | Exercise 23 | Python for Beginners #lec68
5.0 / 5 (0 votes)