00:29Python offers many different ways to organize collections of data items such as string, list, dictionaries, etc.
00:39Let us learn how data items are organized in a Python dictionary, how to access items from a dictionary, etc.
00:46A real-life dictionary holds words and their meanings likewise.
00:51A Python dictionary holds key-value pairs.
00:54Python's Dictionary are known as Mappings.
00:57Mappings are also a collection of different objects, but they use keys to store an object instead of its relative index or position.
01:05A dictionary is an unordered list of key-value pairs.
01:11The keys in a dictionary are immutable objects like strings or numbers.
01:16The values are mutable and can be modified and changed any time.
01:23Note, if you try to give a mutable type as a key, then Python will give an error as unhashable type.
01:31Dictionaries are used to create a record-like data structure where values stored in it are associated with key values.
01:41How is list different from the dictionary?
01:43The main distinguishing feature between a list and a dictionary would be that instead of the index, we use key to access the elements of a dictionary.
01:55Dictionary in Python is defined inside curly braces.
02:00Each key is separated from its value by a colon.
02:03For example, consider a dictionary with three items with keys, vehicle, price and color.
02:13Then we can create a dictionary like this.
02:15The association of key and a value is called a key-value pair.
02:19Like vehicle is a key that is mapped with two wheelers and so on.
02:24D is a dictionary name.
02:25The key-value pairs of the dictionary are separated by commas.
02:29When we print the dictionary, the order of the pairs is not the same.
02:35This will create an empty dictionary named d.
02:38It represents a dictionary with no items.
02:42Dict is a built-in function to create a dictionary.
02:46To add an element to the dictionary d, we can use square brackets for initializing and accessing dictionary values.
02:54These pairs are added to the dictionary d.
02:57The keys are inside the square brackets.
02:59The values are located on the right side of the assignment.
03:03Line creates an item that maps from the key sun to the value sunday and so on.
03:11The elements are accessed through the keys defined in the dictionary.
03:15Keys acts like indices to access values from a dictionary.
03:19Traversing a dictionary means accessing and processing each element in it.
03:27We can iterate through the Python dictionary using for loop.
03:31For loop makes it easy to traverse the items in a dictionary.
03:35The output of the above code is
03:40The loop variable key in the above loop will be assigned the keys of the dictionary d.a one at a time.
03:50The corresponding value is printed along with the key inside the loop body through a statement.
03:55Dictionary name dot keys function is used to access all the keys in a dictionary in one go.
04:03Dictionary name dot values function is used to access all the values in a dictionary in one go.
04:10What is printed by the following statements?
04:136 is associated with the key green.
04:17When the square bracket used with a dictionary, the Python will look up a value based on its key.
04:23The characteristics of a dictionary are as follows.
04:28Unordered set.
04:29Dictionaries are an unordered set of elements.
04:33Its values can contain references to any type of object.
04:39Not a sequence.
04:40A dictionary is not a sequence because it is an unordered set of elements.
04:45The sequences are ordered and indexed by a range of ordinal numbers.
04:50Indexed by keys, not numbers.
04:54Dictionaries are indexed by keys.
04:57The keys of a dictionary must be of immutable types.
05:01The values in a dictionary can be of any type.
05:04The given example has keys of different immutable types.
05:09Keys must be unique.
05:11Each value must have a unique key since a key is used to uniquely identify each element of the dictionary.
05:18However, two unique keys can have the same value.
05:22For example, notice that two different keys, A and B, have same value, 15.
05:29Mutable.
05:30Dictionaries are mutable, which means they can be changed.
05:34We can change the value of a certain key in place using the assignment statement.
05:38In the given example, the value for key, read, changed using assignment statement.
05:47Internally stored as mappings.
05:49The key, value pairs of a dictionary are associated with one another with some internal function called a hash function.
05:57The way of linking is called mapping.
06:00Let us recapitulate the topic.
06:04A dictionary is an unordered set of key-value pairs where each element is accessed by a unique key.
06:12It creates a dictionary by putting key or value pairs inside the curly braces.
06:18The association of key and a value is called a key-value pair.
06:22Python dictionaries are also known as associative arrays or hash tables since they associate a key with a value.
06:31We can also create a dictionary during runtime.
06:34This way of creation is called dynamic allocation.
06:39Let's go through some project work.
06:42Create a dictionary to hold information about the census.
06:50Each key is a country name and each value is the population of the country.
06:56Create a dictionary where each key is an employee name and each value is that employee designation.
07:05Store at least five pairs in a dictionary.
07:07Use A for loop to print out the employee's name and their designation.
07:15Write a program to input the total number of sections and class teacher's name in 11th class and display all information on the output screen.
07:37Get rid of this辦
07:42The way can I see if you find it with information on appointments on theіль guys' name and off
07:45the distribution of the population.
07:47Add,лад,
07:49move it into account that we have a multi- Buckethead
07:52arrows at least had an infinite amount of spells to the normal ideas around the population.
07:54Let's look at that point out to one thing.
07:57links to two Charles Mall,
07:59listen to one.
08:02Marish of the list and read it into two sentences.
Be the first to comment