Write a function called swappy:
print(swappy(['a', 'b', 'c']))
# prints ['b', 'c', 'a']
Here's one way to do it…
def swappy(my_list):
# create a copy of the original list
new_list = my_list[:]
if len(new_list) >= 2:
new_list[0], new_list[1] = new_list[1], new_list[0]
return new_list
Write a function called average_point:
print(average_point([[2, 2], [3, 1], [4, 0]]))
# prints out [3.0, 1.0]
Nested lists can be tricky sometimes, but here's a solution that could work:
def average_point(points):
x_total = 0
y_total = 0
for p in points:
x_total += p[0]
y_total += p[1]
return [x_total / len(points), y_total / len(points)]
Using the same list of points (a list of lists) as the previous example, write a function called all_y:
print(all_y([[2, 200], [3, 1], [4, 9]])) # --> [[2, 200], [4, 99]]
def all_y(points):
new_points = []
for point in points:
if point[1] > point[0]:
new_points.append(point)
return new_points