From the course: Programming Foundations: Beyond the Fundamentals

Creating more complex collections - Python Tutorial

From the course: Programming Foundations: Beyond the Fundamentals

Creating more complex collections

- Sometimes simply grouping data doesn't meet my organizing needs. For instance, say I've organized all my papers for a project into stacks, receipts, correspondence, and forms. Without a label, it can be hard to remember what's in which pile, and I certainly just can't stack it all up together without losing my organization. With papers, I can use a system like hanging file folders and I can indicate the category on the file folder tab. When I'm done with the box, I have all the documents grouped and labeled for easy access, but stored within a single project, like my papers, related data in my program sometimes needs a more complex approach to organization as well. Many programming languages support a rich collection type that lets you store related information but with a label for each item. In Python, the dictionary data type serves this function. To create a dictionary, I specify a variable name and close the value in curly braces. And then for each item in the collection I provide a tag name followed by a colon and a value with commas separating the different values. In the dictionary.py file, I have four variables which store four of the state symbols of California, the bird, the animal, the flower, and the fruit. These are all state symbols and so it would make sense to group them together but I want to retain information on what type of symbol each value is. And I can do this with a dictionary. So I'll comment out the existing variables and I can use a shortcut for this. So I'll start by selecting the text and then most editors support command slash as a keyboard shortcut to comment out selected code. Then below, I'll make a new variable called California Symbols. And for its value, I'll type an opening curly brace. My editor provides the closing curly brace and when I press enter, I automatically start in a new line with an indent ready for me to add some content. For my first value, instead of state bird, I'll just use the name bird because the variable name already makes it clear that this is a state symbol. Then I'll type a colon and I'll type the value which is California quail as a string. After that closing quote, I type a comma and then press Enter. So the first value has a label of bird and a value of California quail, and that comma at the end separates it from the value that follows. So I'll repeat this for the remaining three values. So using the string animal for the second one, a colon. Then I'm just going to copy and paste. So I'll grab grizzly bear, paste that in, type a comma. And on a new line, flower, I will copy California poppy and paste that in, add a comma. And then finally, fruit, colon. And I'll just type avocado and put a comma at the end for good measure. Like a list, I have all my values grouped together in a single variable but unlike a list, in this dictionary I can still label each item providing more information and giving me more flexibility in how I work with the data.

Contents