Previously, we mentioned several data structures strings, lists and tuples They have strong abilities and good inclusiveness In addition to good inclusiveness they are convenient to use Then, why do we still need the data structure of dictionary Let's have a look Let's turn to Page 100 I see the Chinese character meaning "electricity" and phrases containing this Chinese character include computer, electron, lightning, and I had an electric shock and turn to Page 200 next and turn to Page 300 next Good We see the Chinese character meaning "join" Phrases containing this Chinese character include alliance, joint conference couplet, first line of couplet, and second line of couplet etc If we want to put phrases containing the Chinese character "electricity" and those containing "join" together what should we do We may use lists However, with lists some connections between words or between phrases may get lost If we can include words/phrases related to "electricity" into a whole and those related to "join" into another whole and if they are put together that would facilitate our understanding and subsequent processing Well, how can we express such structures It's the dictionary we're going to talk about today One example first The HR department of a company asks the technical department to create a simple employee information sheet with Python including information like employees' names and salaries And, inquire for its employee – Niu Yun's salaries from the information sheet Previously, two lists were adopted one for storing employees' names and the other for storing employees' salaries Between the two, there is such a relation of correspondence We may use this form "names.index('Niuyun')" to search for Niu Yun's salary The output result is 2000 Wow, the difference is so huge from another person whose first name is also Yun! Do you feel this form a little bit unnatural How about this Do you feel it a little better In such a form, the dictionary we're talking about is used A dictionary may establish mapping between objects What is a dictionary The dictionary is the only built-in mapping type in Python Each element may form a key-value pair yes, the key and the value Among them, a key may be from some immutable types like numbers, strings or tuples but the list, as mentioned before, is impossible When indexing is required, use the key Different from a list the values in a dictionary are unordered only related to the key We talked about the sequence before To quote objects in a sequence, which you might still remember is with the index like list1[1] We use indexing to quote an object With the dictionary we may quote the corresponding value with the key That's the concept of dictionary Next, let's look at how to create a dictionary The first way is to create a dictionary directly represent it with the braces with commas in between to separate elements which is similar to the previous creation of objects What's special here is in the dictionary, colons are used between keys and values to form mapping relations That's the first mode of direct creation The other way is with the dict() function For example, here is a list "info" We may use the dict() function to create a dictionary Its result is like this We may also directly write down the list Then, as we see what are the differences between this and that above As for the elements of list they are tuples here and, here, we see lists Both work Besides, we may use this form It's special. Equal Creating a dictionary in this way we may also get the same result Here, let's boldly speculate that if we use dict() followed by tuples elements of tuples also being tuples does that also work Let's think about it I guess it should also be OK The reason is, in this way their relation of mapping between each other should be quite clear So, the system must be able to process it Have a try We'd like to use the dict() function to convert tuples into a dictionary elements of tuples also being other tuples like ('Wangdachui': 3000) ('Niuyun': 2000) Does it work? No error It really works In this instance we've found that, if there is correspondence relation between an element and another we may use dict() to convert them into a corresponding key and a value to generate a mapping relation i.e., the key-value pair Let's look at another question when creating the employees' information sheet how can we set the default values of all employees' salaries to be 3000 It is a question of creating a dictionary If with the previous method we may create one like this: "name:3000, name:3000" However, it seems too troublesome We may use the function of fromkeys() to put all keys together Put their shared value after them One time of writing the value will suffice In this way, we see the generated results It's such a dictionary There's one question for you can the form of tuple here be changed into square brackets That's to say, the form of a list In fact, they can The first argument "S" in the function "fromkeys(S[,v])" only requires a form of sequence A list sure is However, the element in S is required to be immutable Let's look at this question sorted(dictionary) Why this A dictionary stores information in an unordered way so the result of "sorted()" may differ from what we imagined Let's look at its result The returned result is a list, instead of a dictionary It actually is an order stored inside the key It's also something very special Well, another question If the list of names and the list of salaries have existed how can we generate an employee information sheet in the type of dictionary As we mentioned before Once we combine data into a set we can use the dict() function to convert it into a dictionary Then, how can we combine the elements corresponding to the two lists, respectively Have you got any idea As we mentioned earlier, there's a zip() function suitable for the sequence and it has such an ability We use the zip() function to pack data corresponding to the two lists or, like a zipper to make them interlocked with each other Then, use the dict() function to convert them into a dictionary The zip() function is frequently used for generating dictionaries You'd better remember it Let's look at another example of generating a dictionary from existing data with financial data of several companies how can we construct a dictionary containing company codes and stock prices The form of financial data is like this This data set was generated by us with a program before in the form of list The form of dictionary to be generated is like this It only needs company codes and the latest trading prices To complete this task, would it be OK to do like this take out the second-level element of each element in the list 0 and 2, and put them into a list and then, use the zip() function to pack the corresponding elements next, use the dict() function to convert them into such a dictionary Well, let's look at the program The "for" loop is for traversing elements in a list and then use the "append" method of list to append these elements into the list Next, use the zip() function to pack them and then, use the dict() function to convert them into our desired dictionary, correspondingly Here, let's go on thinking about the program writing we mentioned just now which utilizes the concept of iteration of sequence index The loop variable "i" is the index of each element in the sequence Let's think again If we directly use the concept of iteration of sequence items themselves how can this problem be solved First, it's certain that we must use a statement like "for item in pList" to traverse each element in the list pList Then, how can we constitute a mapping relation between the first and third child elements of each element That depends on, for each item namely, each element, how can the corresponding child element be extracted For example, for the first element, what we'd like to extract is the string AXP Is it indeed item[0] How about the corresponding price Is it item[2] Very good It's easy now to constitute mapping relations So, we only need to define a null dictionary and then it's OK to use such a statement It is possible to encounter these two kinds of thought in daily problem solving Do well understand them In this section, we've introduced the characteristics of dictionary and how to generate dictionaries we'll talk about how to use it in next section