- 6 months ago
Category
📚
LearningTranscript
00:00Working with Dictionaries
00:01The topic contains the following concepts.
00:13Introduction, multiple ways of creating dictionaries,
00:16deleting elements from a dictionary,
00:18checking for existence of a key,
00:21pretty printing a dictionary,
00:23dictionary functions and methods,
00:25recapitulation of a topic.
00:27A dictionary consists of a collection of key-value pairs.
00:33Each key-value pair maps the key to its associated value.
00:37A dictionary is written as a sequence of key or value pairs
00:41separated by commas enclosed in curly braces.
00:45There are different ways of creating dictionaries.
00:48Let us discuss here in detail.
00:51In this method, all the key, value pairs of a dictionary
00:55are written collectively,
00:58separated by commas and enclosed in curly braces.
01:02In this method, firstly, create an empty dictionary
01:06and then keys and values are added to it.
01:10We can also create a dictionary using the built-in function dict.
01:14This will create an empty dictionary.
01:16The syntax will add key, value pairs, to a dictionary.
01:22This method of creating dictionaries is used to create dictionaries
01:26dynamically at runtime.
01:30The keys are given as arguments.
01:33Python automatically converts argument names to string type keys.
01:38In this format, we need to enclose the key, value pairs in curly braces as shown.
01:46The key values are given as arguments to the zip function
01:50and enclosed separately in parenthesis.
01:54The zip function are given as argument of dict.
01:58The zip function clubs first value from the first set
02:02with the first value of the second set and so on.
02:05The dict creates key, value pairs for the dictionary.
02:11In this format, one list or tuple argument is passed to dict.
02:16This argument contains lists or tuples of individual key, value pairs.
02:23We can add new elements, key-value pair,
02:27to a dictionary using the assignment operator.
02:30The key must be unique.
02:32If the key already exists in the dictionary,
02:34then the statement will change the value of the existing key
02:38and no new entry will be added to the dictionary.
02:42In dictionaries, the updation and addition of elements are similar in syntax.
02:50Dil command is used to delete a dictionary element that is a key-value pair.
02:55Consider the following example.
02:57Here we see that entry is removed.
03:00If the key does not exist in the dictionary,
03:03then Python will raise an exception called key-error.
03:08POP method is also used for deleting elements from a dictionary
03:12and returns the corresponding value.
03:15Consider the given example.
03:18In the given example, the value shown here for the element deleted.
03:22This method does not raise an exception when the given key does not exist in the dictionary.
03:29This can be done as per the following syntax.
03:32This statement will return the specified message in an argument in place of an error message.
03:39Storing a dictionary inside another dictionary is called nesting of dictionaries.
03:44In Python, a nested dictionary can be created by placing the comma separated dictionaries enclosed within braces.
03:54Here, the nested dict is a nested dictionary with the dictionary dict-a and dict-b.
04:01They are two dictionaries each having own key and value.
04:05We need to store a dictionary as a value only inside a dictionary.
04:10We cannot have a key of dictionary type.
04:13Only immutable types can form the keys of a dictionary.
04:18We can test if a key is in a dictionary or not using the keyword in and not in.
04:25The membership test is for keys only, not for values.
04:28Not in operator will return true if the given key is not present in the dictionary, otherwise false.
04:44These operators do not apply on values of a dictionary.
04:47If we look for a value in a dictionary, use in operator with dictionary name dot values, that is.
04:55The default way of printing dictionary in Python are given below.
05:01It will display the keys and values.
05:04When the dictionary is too large, then the user should need to print it more readable and presentable.
05:11To print the dictionary more presentable, we need to import json module by giving statement import json and then use the function dumps of json module.
05:22In the given example, the key value pairs are displayed in separated lines and two spaces in from every line because we gave value 2 for indent.
05:35Python has various built-in methods and functions to manipulate Python dictionaries.
05:41The len method returns the length of the dictionary.
05:45The syntax to use this method is given below.
05:48It takes a dictionary name as an argument and returns an integer.
05:51The clear method removes all the elements inside the dictionary.
05:57The syntax to use this method is given below.
06:00It takes no argument, returns no value.
06:04The get method returns a value for the specified key.
06:08It works like dictionary keys.
06:12The syntax to use this method is given below.
06:15It takes key as essential argument and returns corresponding value.
06:19If the key does not exist in a dictionary, then the second parameter of the method is returned.
06:27The item's method returns a list of key value tuples.
06:31The syntax to use this method is given below.
06:34It takes no argument and returns a sequence of key value pairs in no particular order.
06:40The keys method returns a list of keys from a dictionary.
06:45The syntax to use this method is given below.
06:48It takes no argument and returns a list sequence of keys.
06:53The values method returns a list of values from the dictionary.
06:59The syntax to use this method is given below.
07:03It takes no argument and returns a list sequence of values is not in particular order.
07:09The update method merges the keys and values of one dictionary into another and overrides the values of the same key.
07:18The syntax to use this method is given below.
07:22Dictionary 2 is merged with dictionary 1.
07:25In the given example, the elements of dictionary fruits 2 have overridden the elements of dictionary fruits 1 having the same keys, that is, of keys apples and oranges.
07:36Let us recapitulate the topic.
07:41We can also create a dictionary during run time.
07:44This way of creation is called dynamic allocation.
07:48The update method merges the keys and values of one dictionary into another and overrides values of the same key.
07:56The dil statement removes a key value pair from a dictionary.
08:01The item's method returns a list of key value tuples.
08:05Write a program to take the number of students as input, then ask marks for 5 subjects as English, Physics, Chemistry, Maths and Computer.
08:18If the total marks for any student is less than 300, then print, he failed, else print, passed.
08:24Create an empty dictionary called prices and stock.
08:33Add the values in a prices and stock dictionary.
08:36Loop through each key in prices.
08:38For each key, print out the key along with its price and stock information.
08:43Now, create a variable called total and set it to 0.
08:48Loop through the prices dictionaries.
08:49For each key in prices, multiply the number in prices by the number in stock.
08:55Print that value and then add it to total.
08:59Now print the total.
09:19That is going to be a price range of $1350,000.
09:29And take the number of students to choose $1550,000.
09:30For each key, print that value and set it to $1550,000.
09:32If you consider that $1550,000, you want to have an increase in price of $1550,000.
09:34Then put them in the prices of $1550,000.
09:36If you consider that $1550,000 is $1550,000.
09:38Then put them in $5050,000.
09:39If you want to have $1550,000.
09:41Should be $1550,000 for $1550,000.
Recommended
5:09
|
Up next
8:23
1:14:58
0:30
35:30
7:54
9:10
16:42
16:38
7:52
10:07
6:38
9:50
6:19
19:04
8:02
13:05
15:17
10:32
11:11
9:57
5:08
7:20
5:48
Be the first to comment