Code Monkey home page Code Monkey logo

python-dictionaries-lab-ds-apply-000's Introduction

Dictionaries lab

Introduction

In this lesson, we'll use our knowledge of dictionaries to retrieve data about various cities.

Objectives

  • Practice retreiving information from dictionaries
  • Practice assigning new information to dictionaries
  • Practice retriving information from a list of dictionaries

Working with a single dictionary

Here is a dictionary representing the city of Greenville, North Carolina. The area is in kilometers squared.

greenville = {'Area': 68, 'City': 'Greenville', 'Country': 'USA', 'Population': 84554}

Remember to press shift + enter to run the code.

Let's retrieve the population of the city and assign it to the variable greenville_population.

greenville_population = None # change None
greenville_population # 84554

Now retrieve the area of Greenville and assign it to the variable area.

area = None
area # 68

Now let's take a look at all of the keys in the greenville dictionary and coerce them into a list. Assign this variable to the list city_keys.

city_keys = None
city_keys # ['Area', 'City', 'Country', 'Population']

Alright, next let's get all of the values in our greenville dictionary and coerce it into a list. Assign that list to the variable city_values.

city_values = None
city_values # [68, 'Greenville', 'USA', 84554]

Working with multiple cities

Once again, we can retrieve our data from a Google Sheet of Travel Cities and Countries shown here.

We already followed the steps of the previous lesson to download the spreadsheet and move it to the current folder. You can find the file in the github repository. So next, we get this data into Python code. We have written the code for reading excel into Python for you.

import pandas
file_name = './cities.xlsx'
travel_df = pandas.read_excel(file_name)
cities = travel_df.to_dict('records')

Remember to press shift + enter.

Cool. We have them!

cities
[{'City': 'Solta', 'Country': 'Croatia', 'Population': 1700, 'Area': 59},
 {'City': 'Greenville', 'Country': 'USA', 'Population': 84554, 'Area': 68},
 {'City': 'Buenos Aires',
  'Country': 'Argentina',
  'Population': 13591863,
  'Area': 4758},
 {'City': 'Los Cabos',
  'Country': 'Mexico',
  'Population': 287651,
  'Area': 3750},
 {'City': 'Walla Walla Valley',
  'Country': 'USA',
  'Population': 32237,
  'Area': 33},
 {'City': 'Marakesh', 'Country': 'Morocco', 'Population': 928850, 'Area': 200},
 {'City': 'Albuquerque',
  'Country': 'New Mexico',
  'Population': 559277,
  'Area': 491},
 {'City': 'Archipelago Sea',
  'Country': 'Finland',
  'Population': 60000,
  'Area': 8300},
 {'City': 'Iguazu Falls',
  'Country': 'Argentina',
  'Population': 0,
  'Area': 672},
 {'City': 'Salina Island', 'Country': 'Italy', 'Population': 4000, 'Area': 27},
 {'City': 'Toronto', 'Country': 'Canada', 'Population': 630, 'Area': 2731571},
 {'City': 'Pyeongchang',
  'Country': 'South Korea',
  'Population': 2581000,
  'Area': 3194}]

Ok, so the list of countries associated with each city has been assigned to the variable cities. Now we will work with reading and manipulating this list of cities.

Working with our list of cities

First, access the third to last element and set it equal to the variable salina.

salina = None 
salina
# {'Area': 27, 'City': 'Salina Island', 'Country': 'Italy', 'Population': 4000}

Now access the fourth country in the list, and set it's population equal to a variable called los_cabos_pop.

los_cabos_pop = None
los_cabos_pop # 287651

Now calculate the number of cities in the list and assign the number to the variabale city_count.

city_count = None
city_count # 12

Finally, change the spelling of the South Korean city, Pyeongchang, to the string 'PyeongChang', its alternative spelling.

cities[11]['City'] # 'PyeongChang'

Now let's work on retrieving a collection of information about a dictionary. Use the appropriate dictionary function to return a list of values in the dictionary regarding Pyeongchang. Assign the list to the variable pyeongchang_values.

pyeongchang_values = None

pyeongchang_values # ['PyeongChang', 'South Korea', 2581000, 3194]
type(pyeongchang_values) # list
NoneType

And now set pyeongchang_keys equal to a list of keys in the dictionary regarding Pyeongchang.

pyeongchang_keys = None


pyeongchang_keys # ['City', 'Country', 'Population', 'Area']
type(pyeongchang_keys) # list
NoneType

Summary

In this section we saw how to retrieve and alter data in a dictionary. We saw how we can retrieve a collection of information about a dictionary, like a list of it's keys and values, and we saw how to work with a list of dictionaries.

python-dictionaries-lab-ds-apply-000's People

Contributors

jeffkatzy avatar tkoar avatar

Watchers

James Cloos 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.