Giter VIP home page Giter VIP logo

python-dictionaries-readme-data-science-intro-000's Introduction

Dictionaries

Introduction

After introducing and working with Lists, we must be wondering if there are other kinds of collections in Python that we should know about. Well, there are! In this lesson, we will introduce Dictionaries. As we know, lists represent a collection of information that is ordered, like a list of the most watched TV shows. However, in different situations, we may want our data to represent attributes of an entity, for example, the various attributes of a single TV show like its name, genre, starring actors, etc. For this, we can use a dictionary.

Learning Objectives

  • Learn to decide when to use a dictionary or a list
  • Understand how a dictionary allows us to represent attributes with keys and values
  • Understand how to read a value from a dictionary
  • Understand how to assign a key-value pair to a dictionary

Why Use a Dictionary When We Have Lists?

Okay, so, in this lesson we are going to be introducting not only a new type of collection in Python, but also a new datatype. Why? We already have lists, which we know we can use to keep groups of related data together. What is so great about dictionaries? Well, while lists are great, for listing information like we mentioned earlier, they can actually become very messy when we are trying to use them to organize data which is more a bit more complex. Let's look at a brief example of a person.

Every person has a name, age, height (in inches), weight (in lbs), and fav_lang. How we would represent a person using a list?

terrance = ["Terrance", 25, "6'00", 165, "Python"]

Now, that looks fine but what do we do if we want to tell someone terrance's fav programming language? We just have to remember that terrance's favorite programming language comes fifth in his list of information? What if he has more attributes than just the five that are listed (i.e. native_language, hometown, etc.)? What if his attributes are in a different order than we expected? We can see that this list would easily breakdown and cause more problems than it solves.

However, if we use a dictionary, we can more neatly organize this information and make it easier for us to use as the dictionary grows. Let's see what terrance's information would look like using a dictionary.

terrance = {'name': "Terrance", 'age': 25, 'weight': 72, 'height': 165, 'fav_lang': "Python"}

This dictionary definitely has more text in it, but we can see a direct association between the attribute or key and its correlated value (i.e. {'key': "value"}). This datatype makes it easier to store and access information, such as the attributes of a person or other entity.

Let's take a deeper look at how dictionaries are built and how they work.

Creating a dictionary, and retrieving attributes

Imagine we want to represent information about the TV show Friends. Our first step might be to go to Wikipedia to find some information.

As you can see, this information is presented in two columns, with the topics or headings to the left and their specific values to the right. Now let's see how some of the above information can be represented as a dictionary in Python.

friends = {'name': 'Friends', 'genre': 'sitcom', 'no_of_seasons': 10}

We create a dictionary with the braces, also called curly braces. (On your keyboard, braces are located above the return key). A dictionary is a group of key and value pairs, with the key to the left and the corresponding value to the right.

Now that we have initialized a dictionary and assigned it to the variable, friends, we can retrieve the dictionary by referencing our variable.

friends
{'name': 'Friends', 'genre': 'sitcom', 'no_of_seasons': 10}
friends['no_of_seasons']
10

So to retrieve a specific value, we simply reference the dictionary, then the brackets, then the specific key. The corresponding value is returned.

Assigning attributes and exploring the edge cases

Now that we know how to retrieve values, let's take our existing friends dictionary, and assign it more key-value pairs. Here is what our dictionary currently looks like.

friends
{'name': 'Friends', 'genre': 'sitcom', 'no_of_seasons': 10}

Let's add a key of no_of_episodes with a value of 236.

friends['no_of_episodes'] = 236
friends
{'name': 'Friends',
 'genre': 'sitcom',
 'no_of_seasons': 10,
 'no_of_episodes': 236}

So as you can see, our values of a dictionary can be any data type -- strings, numbers, and others. How about keys? Do keys have to be strings? With programming, we don't always have to look up the answer. We can just try things.

friends[14] = 'some value'
friends[14]
'some value'

Apparently keys can also be integers.

Ok, let's get rid of that key - it doesn't make much sense.

del friends[14]

We use the delete function, del, followed by the dictionary and the name of the key. And now the key-value pair is gone.

friends
{'name': 'Friends',
 'genre': 'sitcom',
 'no_of_seasons': 10,
 'no_of_episodes': 236}

Dictionaries with lists

If you look back up at our Friends table, you will see that there are two creators. It probably makes sense to think of these creators as a list.

creators = ['David Crane', 'Marta Kauffman']

So let's have our friends dictionary have a key of creators that points to this list.

friends['creators'] = ['David Crane', 'Marta Kauffman']
friends
{'name': 'Friends',
 'genre': 'sitcom',
 'no_of_seasons': 10,
 'no_of_episodes': 236,
 'creators': ['David Crane', 'Marta Kauffman']}
friends['creators']
['David Crane', 'Marta Kauffman']

And of course, if we want to get the first creator in the list, and store it as a variable, we can.

david = friends['creators'][0]

So in the above line, we referenced the dictionary, then got to the list of creators through using the key creators. And now that we are pointing to that list, we use the brackets to reference the string at index zero.

Lists of Dictionaries

Now imagine we want to represent another TV show.

As you can see, Wikipedia provides us data similar to what we have for Friends.

friends
{'name': 'Friends',
 'genre': 'sitcom',
 'no_of_seasons': 10,
 'no_of_episodes': 236,
 'creators': ['David Crane', 'Marta Kauffman']}

So let's represent our information for Seinfeld in a dictionary.

seinfeld = {'name': 'Seinfeld', 'creators': ['Larry David', 'Jerry Seinfeld'], 'genre': 'sitcom', 'no_of_seasons': 10, 'no_of_episodes': 180}
seinfeld
{'name': 'Seinfeld',
 'creators': ['Larry David', 'Jerry Seinfeld'],
 'genre': 'sitcom',
 'no_of_seasons': 10,
 'no_of_episodes': 180}

Now that we have two TV shows, we can envision that having a list of TV shows.

tv_shows = [friends, seinfeld]
tv_shows
[{'name': 'Friends',
  'genre': 'sitcom',
  'no_of_seasons': 10,
  'no_of_episodes': 236,
  'creators': ['David Crane', 'Marta Kauffman']},
 {'name': 'Seinfeld',
  'creators': ['Larry David', 'Jerry Seinfeld'],
  'genre': 'sitcom',
  'no_of_seasons': 10,
  'no_of_episodes': 180}]

This is a nested data structure. And it can be confusing to disentangle. A good technique is to describe the data structure first before working with it.

So tv_shows is a list, with each element of the list being a dictionary. The dictionary has a key of creators which itself points to another list. In describing the data structure, we look to the braces and brackets at the beginning. [{ means we are starting a list with a dictionary as the first element.

Ok, now let's start working with this nested data structure. First let's select the second creator of Seinfeld and set it equal to the variable jerry. We'll retrieve this data in steps. First, we'll select the correct TV show.

tv_shows[1]
{'name': 'Seinfeld',
 'creators': ['Larry David', 'Jerry Seinfeld'],
 'genre': 'sitcom',
 'no_of_seasons': 10,
 'no_of_episodes': 180}

Now we have the correct TV show. Let's keep going.

tv_shows[1]['creators']
['Larry David', 'Jerry Seinfeld']

Ok, almost there, we have our list of creators.

tv_shows[1]['creators'][1]
'Jerry Seinfeld'

So as you see above, we are now selecting the correct creator from the list.

jerry = tv_shows[1]['creators'][1]
jerry
'Jerry Seinfeld'

Ok so our approach here was to break this problem down into steps. We first selected the correct TV show. Then, we moved onto the creators attribute. Finally, we retrieved the correct element from the list of creators.

As programmers, we tend not to get much smarter over time. Instead, we develop skills for making problems easier to solve. Taking the problem in steps, and checking our work at each of these steps is a technique we should continue to lean on. It's the mark of a skilled developer.

Summary

In this section, we saw a new type of collection, the dictionary. A dictionary is a sequence of key-value pairs. We mark the start and end of a dictionary with curly braces, {}, and then follow the pattern of 'key':'value' for each of the associated attributes, with each attribute separated by a comma: dictionary = {'key1':'value1', 'key2':'value2'}.

We retrieve a specific value from a dictionary by using the bracket accessor in combination with the key, so dictionary['key2]' returns 'value2'. And we add a new attribute with the format dictionary['key3'] = 'value3'.

Finally, we saw that we can represent data as nested data structures. In working with nested data structures a good technique is to pay attention to the edges of the data structure as in [{, and then articulate how that data structure is nested. Finally, when accessing data from a nested data structure, it is useful to break down the problem into steps to get feedback along the way.

python-dictionaries-readme-data-science-intro-000's People

Contributors

cutterbuck avatar jeffkatzy avatar talum avatar tkoar avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.