From the course: Programming Foundations: Beyond the Fundamentals

Working with collections

- Both lists and dictionaries allow me to store related data using a single variable name. To access and use individual pieces of data in either of these collection types is slightly different than simply referencing a variable name, but it's pretty straightforward to do with the proper syntax. Each item in a list is automatically assigned a consecutive number based on its position, known as an index number. It's important to note that index numbering does not start at one, it starts at zero. This system is common in a lot of programming languages and working with list indexes is a great way to get your feet wet with it. Within a list, the first item has an index of zero. The second item has an index of one, and so on. To reference the data at a given list index, you reference the variable name followed by the index number in square brackets. In my list.py file, my cities list contains four pieces of data, strings that represent city names. The first string, Tokyo, will have an index value of zero, and the last string, Buenos Aires, will have an index of three. I want to print the second city name, Dakar, to the terminal. So below my variable, I'll type the print command and open parentheses, and my editor shows me documentation, which I can dismiss by hitting escape. Then in the parentheses, I reference the variable name that stores the list, cities, and then in square brackets, the index number for the second piece of data. Now, because index numbers start at zero, Tokyo would be zero, and Dekar would be one. So I put one in the square brackets, then I save my code, and then I'll run it. And there, in my terminal, Dekar is printed. Referencing data in a dictionary is similar to a list reference. To reference a dictionary item, I start by referencing the name of the variable that stores the dictionary value. A dictionary doesn't use index numbers though, because each item in a dictionary has a label. I'm going to switch to the dictionary.py file. To reference a dictionary value, I simply add square brackets after the variable name and specify the label in quotes. I want to print the name of the California state flower to the terminal. That's stored in my California symbols dictionary with the label flower. So I can add a print statement, open those perens, and press escape to get rid of that extra documentation. Then I'll type the name of the variable, California symbols, and once I start typing, my editor automatically prompts me what it thinks I'm typing. In this case it's right, and I can simply press tab to autocomplete that. Then I'll open my square brackets, and in those square brackets, instead of a number, I just need the label name, which is the string flower in quotes. Then I can save that. I can run my code. And now, California poppy is printed to the terminal. Collections provide a way of creating organized code, while still allowing you to work with individual values within them pretty easily. Collections also lend themselves to programming techniques such as iteration, which lets you easily work with all the values in a collection at once. Being able to work with data in collections adds a really useful tool to your programming toolbox.

Contents