Ad
  • Custom User Avatar
  • Custom User Avatar

    Thank you for your review and feedback of my code, I appreciate it. I didn't know an empty list would be skipped by default and my if statement does have duplicate code.

    I'll refactor for sure. Thanks again!

  • Custom User Avatar

    I already see one flaw right away. For this piece of code, you do not need to put 'if len(numbers) < 0:return numbers' because the for loop will not be activated by an empty list, and it will just simply skip it.

    Another one is that the if statements in the for loop are repetitive. There, you will see that no matter where the number goes in the if statement, if will always be multiplied by -1. So, you may simply state: 'for i in numbers: opposite_list.append(i * -1)' which saves much time since the computer still needs to check for a condition.

    Last one. If you don't know list comprehensions, you may search it up. It is really useful for this problem. Since you are iterating through the list with just one line of code inside the loop, you may simply do: 'return [i * -1 for i in numbers]' and that is it.

    So hope you learned something, bye!