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 = []
In [5]:
from legislators import legislators
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)
In [9]:
for leg in legislators:
if leg[3] == '':
leg[3] = 'M'
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)
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])
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.
In [15]:
for i, e in enumerate(legislators):
e.append(birth_years[i])
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)
In [ ]: