When working with data, we need ways to store it in variables so we
can manipulate it. We will use two new data structures that we didn't
cover in Introduction to Programming. These are lists and dictionaries.
(This chapter contains the same content as the "Data Structures in Python" chapter of the "Websites with Python Flask" workshop.)
If you are already comfortable with lists and dictionaries then you can skip this and move on to the next chapter.
But what if we need something more complicated, like a shopping list?
Assigning a variable for every item in the list would makes things very
complicated:
When you are in the Python interpreter you can see what is inside a list by just typing the name of the list. For example:
The interpreter shows us that the list is empty.
Now we can add items to
What is in the shopping list? What happens when you append numbers or booleans to the list?
You can also assign a list with some items in it all in a single line, like this:
To remove an item from the list we use
Lists can easily be processed in a
And that's it! Python also makes it really easy to check if something is in a list or not:
Lists are the most common data structure in programming. There are
lots of other things you can do with lists, and all languages have their
own subtly different interpretation. But fundamentally they are all
very similar.
In summary:
You can create a dictionary with
And you can add items to the dictionary like this:
The keys in this example are "banana" and "dirt", and the values are
the things that we assign to them. You can use any data type that won't
change as a dictionary key. Check it out by using a number, a boolean
value, and a list as keys in a dictionary. What does this say about
strings?
As with lists, you can always see what is inside a dictionary:
You can look up any entry in the dictionary by its key:
If the key isn't found in the dictionary, a
For this reason, you can test whether a key is in the dictionary or not, by using the keyword
You can delete from a dictionary as well. We don't really need to include an entry for dirt:
What makes dictionaries so useful is that we can give meaning to the
items within them. A list is just a bag of things, but a dictionary is a
specific mapping of something to something else. By combining lists and
dictionaries you can describe basically any data structure used in
computing.
For example, you can easily add a list to a dictionary:
Or add dictionaries to lists:
Outside of Python, dictionaries are often called
(This chapter contains the same content as the "Data Structures in Python" chapter of the "Websites with Python Flask" workshop.)
If you are already comfortable with lists and dictionaries then you can skip this and move on to the next chapter.
Understanding data structures
Back in the first session we introduced three of the most common data types used in programming: numbers, strings and booleans. We assigned those data types to variables one-by-one, like so:x = 3 # numbers
a = "gorillas" # strings
t = True # booleans
item_1 = "milk"
item_2 = "cheese"
item_3 = "bread"
Lists
Fortunately we don't have to do this. Instead, we have thelist
data type. An empty list is simply []
shopping_list = []
>>> shopping_list
[]
Now we can add items to
shopping_list
. Try typing the following commands into the Python interpreter.shopping_list.append("milk")
shopping_list.append("cheese")
shopping_list.append("bread")
You can also assign a list with some items in it all in a single line, like this:
shopping_list = [ "milk", "cheese", "bread" ]
remove()
:shopping_list.remove("milk")
for
loop. Have a look at this example which prints each item of the list in a new row:for item in shopping_list:
print(item)
if "milk" in shopping_list:
print("Delicious!")
if "eggs" not in shopping_list:
print("Well we can't have that!")
shopping_list.append("eggs")
In summary:
shopping_list = []
shopping_list.append("cookies")
shopping_list.remove("cookies")
Dictionaries
The other main data type is the dictionary. The dictionary allows you to associate one piece of data (a "key") with another (a "value"). The analogy comes from real-life dictionaries, where we associate a word (the "key") with its meaning. It's a little harder to understand than a list, but Python makes them very easy to deal with.You can create a dictionary with
{}
foods = {}
foods["banana"] = "A delicious and tasty treat!"
foods["dirt"] = "Not delicious. Not tasty. DO NOT EAT!"
As with lists, you can always see what is inside a dictionary:
>>> foods
{'banana': 'A delicious and tasty treat!', 'dirt': 'Not delicious. Not tasty. DO NOT EAT!'}
>>> foods["banana"]
'A delicious and tasty treat!'
KeyError
occurs:>>> foods["cheese"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'cheese'
in
:if "cheese" in foods:
print("Cheese is one of the known foods!")
print(foods["cheese"])
not in
works as well, just like with lists.You can delete from a dictionary as well. We don't really need to include an entry for dirt:
del foods["dirt"]
For example, you can easily add a list to a dictionary:
ingredients = {}
ingredients["blt sandwich"] = ["bread", "lettuce", "tomato", "bacon"]
europe = []
germany = {"name": "Germany", "population": 81000000}
europe.append(germany)
luxembourg = {"name": "Luxembourg", "population": 512000}
europe.append(luxembourg)
hash tables
, hash maps
or just maps
.
No comments:
Post a Comment