For more information on concepts covered in this lesson, you can check out:
Creating Lists and Dictionaries
00:00 Lists and dictionaries. Lists are powerful data structures in Python that often represent a series of related attributes. Similarly, dictionaries are used all over Python and are great for structuring information.
00:16 Sometimes when setting up these data structures, you end up performing the same operation several times. As a first example, calculate some basic descriptive statistics of a list of numbers and store them in a dictionary.
Note that both the sum and the length of the
numbers list are calculated twice. The consequences are not too bad in this simple example, but if the list was larger or the calculations were more involved, you might want to optimize the code. To do this, you can first move the function calls out of the dictionary definition.
num_sum are now defined inside the definition of
description. This is a clear hint to anybody reading this code that these variables are just used to optimize these calculations and aren’t used again later.
Note that the scope of the
num_sum variables is the same in the example with the walrus operator and without. This means that in both examples, the variables are available after the definition of
03:07 Even though both examples are functionally similar, a benefit of using the assignment expressions is that the walrus operator communicates the intent of these variables as throwaway optimizations.
Storing a filename in a
Path object allows you to conveniently read the text file in the next lines. These lines construct a tuple of counts to represent the number of lines, words, and characters in one text file.
Finally, this line prints all three counts together with the filename to the console. The
*counts syntax unpacks the
counts tuple. In this case, the print statement is equivalent to
print(counts, counts, counts, path) as seen onscreen.
The contents of the file are assigned to
text, which is reused in the next two calculations. The program still functions in the same way. As seen in previous examples, an alternative approach is to define
text before the definition of
06:04 While this is one line longer than the previous implementation, it probably provides the best balance between readability and efficiency. The walrus operator isn’t always the most readable solution even when it makes your code more concise.
Become a Member to join the conversation.