Sep-26-2021, 07:18 AM
(This post was last modified: Sep-26-2021, 07:24 AM by OmegaRed94.)
Good day, Dear Pythonistas
I have the following problem: Given a list, we should write a function that returns sorted in descending order unique squares of numbers contained in this list
I have got the solution in the following way, it works correctly:
Could you, please, suggest, how I could write this code or the solution in the shortest possible optimal way (a smaller number of characters)?
I have the following problem: Given a list, we should write a function that returns sorted in descending order unique squares of numbers contained in this list
I have got the solution in the following way, it works correctly:
1 2 3 4 5 6 7 8 |
def squares_list(l): for elem in l: empty_list.extend(elem) new_list = list ( set (empty_list)) new_list.sort(reverse = True ) squares = [i * * 2 for i in new_list] return squares print (squares_list([[ 1 , 2 ], [ 3 ], [ 4 ], [ 3 , 1 ]])) |