2: Find the different genders

Instructions

Loop through the rows in legislators, and extract the gender column (fourth column)

Append the genders to genders_list.

Then turn genders_list into a set, and assign it to unique_genders

Finally, convert unique_genders back into a list, and assign it to unique_genders_list.


In [4]:
# We can use the set() function to convert lists into sets.
# A set is a data type, just like a list, but it only contains each value once.
car_makers = ["Ford", "Volvo", "Audi", "Ford", "Volvo"]

# Volvo and ford are duplicates
print(car_makers)

# Converting to a set
unique_car_makers = set(car_makers)
print(unique_car_makers)

# We can't index sets, so we need to convert back into a list first.
unique_cars_list = list(unique_car_makers)
print(unique_cars_list[0])

genders_list = []
unique_genders = set()
unique_genders_list = []


['Ford', 'Volvo', 'Audi', 'Ford', 'Volvo']
{'Ford', 'Audi', 'Volvo'}
Ford

In [5]:
from legislators import legislators

Answer


In [6]:
genders_list = []

for leg in legislators:
    genders_list.append(leg[3])
unique_genders = set(genders_list)
unique_gender_list = list(unique_genders)

In [8]:
print(unique_gender_list)


['', 'M']

3: Replacing genders

Instructions

Loop through the rows in legislators and replace any gender values of "" with "M".


In [9]:
for leg in legislators:
    if leg[3] == '':
        leg[3] = 'M'

4: Parsing birth years

Instructions

Loop through the rows in legislators

Inside the loop, get the birthday column from the row, and split the birthday.

After splitting the birthday, get the birth year, and append it to birth_years

At the end, birth_years will contain the birth years of all the congresspeople in the data.


In [11]:
birth_years = []

for row in legislators:
    birth_year = row[2].split("-")[0]
    birth_years.append(birth_year)
print(birth_years)


['1745', '1742', '1743', '1730', '1739', '', '1738', '1745', '1748', '1734', '1756', '', '1737', '1754', '1736', '', '1727', '1733', '1732', '1737', '1739', '1734', '1740', '1745', '1728', '', '1738', '1737', '1739', '1744', '', '1761', '1756', '1752', '1737', '1745', '1744', '1742', '1726', '', '1733', '1741', '', '1756', '', '1721', '', '1764', '', '1740', '1745', '1745', '', '1735', '1745', '', '1753', '1748', '1749', '1740', '1746', '1729', '', '', '1726', '1748', '1756', '', '1753', '', '', '1753', '', '1739', '', '1738', '1741', '1750', '1761', '', '1758', '1744', '1759', '1743', '1736', '', '1741', '1739', '', '1745', '1751', '1743', '1754', '1758', '1743', '1764', '1727', '1749', '1751', '1739', '1753', '', '1758', '1752', '', '1755', '1753', '1747', '', '1751', '1752', '1760', '1756', '', '1753', '1754', '1749', '1758', '1743', '', '1741', '1729', '1755', '', '1750', '1760', '1761', '1743', '1757', '1746', '1765', '', '1753', '1728', '', '', '', '', '1759', '', '1740', '1749', '1749', '1748', '1752', '', '', '', '1763', '', '1739', '', '', '1735', '', '1740']

6: Practice with enumerate

Instructions

Use a for loop to enumerate the ships list.

In the body of the loop, print the ship at the current index, then the car at the current index.

Make sure you have two separate print statements.


In [12]:
dogs = ["labrador", "poodle", "collie"]
cats = ["siamese", "persian", "somali"]

# Enumerate the dogs list, and print the values.
for i, dog in enumerate(dogs):
    # Will print the dog at the current loop iteration.
    print(dog)
    # This will equal dog.  Prints the dog at index i.
    print(dogs[i])
    # Print the cat at index i.
    print(cats[i])

ships = ["Andrea Doria", "Titanic", "Lusitania"]
cars = ["Ford Edsel", "Ford Pinto", "Yugo"]

for i, e in enumerate(ships):
    print(e)
    print(cars[i])


labrador
labrador
siamese
poodle
poodle
persian
collie
collie
somali
Andrea Doria
Ford Edsel
Titanic
Ford Pinto
Lusitania
Yugo

7: Create a birth year column

Instructions

Loop through the rows in legislators list, and append the corresponding value in birth_years to each row.


In [14]:
lolists = [["apple", "monkey"], ["orange", "dog"], ["banana", "cat"]]
trees = ["cedar", "maple", "fig"]

for i, row in enumerate(lolists):
    row.append(trees[i])

# Our list now has a new column containing the values from trees.
print(lolists)

# Legislators and birth_years have both been loaded in.


[['apple', 'monkey', 'cedar'], ['orange', 'dog', 'maple'], ['banana', 'cat', 'fig']]

In [15]:
for i, e in enumerate(legislators):
    e.append(birth_years[i])

9: Practice with list comprehensions

Double all of the prices in apple_price, and assign the resulting list to apple_price_doubled.

Subtract 100 from all of the prices in apple_price, and assign the resulting list to apple_price_lowered.


In [16]:
# Define a list of lists
data = [["tiger", "lion"], ["duck", "goose"], ["cardinal", "bluebird"]]

# Extract the first column from the list
first_column = [row[0] for row in data]

apple_price = [100, 101, 102, 105]

In [17]:
apple_price_doubled = [2*p for p in apple_price]
apple_price_lowered = [p-100 for p in apple_price]

print(apple_price_doubled, apple_price_lowered)


[200, 202, 204, 210] [0, 1, 2, 5]

In [ ]: