Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Creating Lists and Dictionaries

For more information on concepts covered in this lesson, you can check out:

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.

00:59 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.

01:52 The variables num_length and num_sum are only used to optimize the calculations inside the dictionary. By using the walrus operator, this role can be made more clear.

02:38 num_length and 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.

02:53 Note that the scope of the num_length and 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 description.

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.

03:20 In the next example, you’ll work with a bare-bones implementation of the wc utility for counting lines, words, and characters in a text file.

03:39 This line loops over each filename provided by the user. sys.argv is a list containing each argument given on the command line starting with the name of your script.

03:53 For more information about sys.argv, you can check out Python Command Line Arguments. This line translates each filename string to a pathlib.Path object.

04:09 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.

04:32 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[0], counts[1], counts[2], path) as seen onscreen.

04:52 To see this code in action, you can use the script on itself as seen onscreen.

05:03 If you look closely at this implementation, you’ll notice it’s far from optimal. In particular, the call to path.read_text() is repeated three times.

05:12 That means that each text file is read three times. You can use the walrus operator to avoid this repetition.

05:34 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 counts.

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.

06:21 In the next chapter, you’ll see the walrus in action when using list comprehensions.

Become a Member to join the conversation.