Stack Exchange
minimize the steps to find out the sum of the square of the numbers which are even and odd
    def sumsquare(l):
      odd = []
      even = []
      for items in l:
          if items % 2 == 0:
             even.append(items)
          else:
             odd.append(items)
      squre1 = []
      squre2 = []
      total1 = 0
      total2 = 0
      for item in odd:
          squre1.append(item ** 2)
      for item1 in even:
          squre2.append(item1 ** 2)
      for i in range(0, len(squre1)):
          total1 = total1 + squre1[i]
      for i in range(0, len(squre2)):
          total2= total2 + squre2[i]
      final_answer =[]
      for j in total1, total2:
          final_answer.append(j)
      print(final_answer)
li = sumsquare([1, 3, 5, 8, 10])

I want to minimize the code. It takes a nonempty list of integers and returns a list [odd,even], where odd is the sum of squares all the odd numbers in l and even is the sum of squares of all the even numbers in l.

So, split by odd/even, compute squares, and sum?

yes, the question is to first find out the odd and even from the given list and then calculate the sum of the squares of both odd and even individually and finally show the result in a [odd, even] format.

Huh -- minimal enough to mention in a comment: li = [sum(x*x for x in l if x & 1),sum(x*x for x in l if not x & 1)]

You can use the builtin sum() function of lists to reduce the code moreover, you can also use list comprehension for making lists as follows.
def sum_square(non_empty_interger_list):
    odd = []
    even = []
    for items in non_empty_interger_list:
        if item%2 == 0:
           even.append(item)
        else:
           odd.append(item)
     even_squares = [x**2 for x in even]
     odd_squares = [y**2 for y in odd]
     even_squares_total = sum(even_squares)
     odd_squares_total = sum(odd_squares)
     final_answers = [even_squares_total, odd_squares_total]
     return final_answers


You shouldn't manipulate variables that you defined outside of the function.
If you gonna run the same function twice it will return two different results because odd and even lists are not empty any more
>> [164, 35]
print(sum_square([1, 3, 5, 8, 10]))
>> [328, 70]


Answer
from typing import Sequence, Tuple


def square(x: int) -> int:
    return x ** 2


def sumsquare(numbers: Sequence[int]) -> Tuple[int, int]:
      odds, evens = [], []
      for number in numbers:
          evens.append(number) if number % 2 == 0 else odds.append(number)
      return sum(map(square, odds)), sum(map(square, evens))


result = sumsquare([1, 3, 5, 8, 10])

Tips

Do not use l (lower-case L) as a variable name as it can be taken for a 1 or a I(upper-case i) in many fonts - this can be found in the PEP8 style guidelines.


you should format your code in a better way,
plus in a function it's better if it returns some value and then you print it
def sumsquare(numbers):
    return [
        sum(map(lambda x: x**2, [i for i in numbers if i%2==1])),
        sum(map(lambda x: x**2, [i for i in numbers if i%2==0]))
    ]
results = sumsquare([1, 3, 5, 8, 10])
print(results)


NIT: you can use generators instead of list-comprehensions (swapping[ and ] by ( and )) and it should be slightly more performant.

Traceback (most recent call last):
File "C:/Users/hp/PycharmProjects/untitled6/pip.py", line 6, in 
li = sumsquare([1, 3, 5, 8, 10])
File "C:/Users/hp/PycharmProjects/untitled6/pip.py", line 3, in sumsquare
sum(map(lambda x: x**2, [i for i in a if i%2==1])),
NameError: name 'a' is not defined
I got an error. Can you help me out to fix it?

I was also thinking on using a list comprehension to filter the nodes but that will traverse the provided sequence twice, while using a loop that appends to two lists will only traverse the sequence once.

@captainsameer sorry I had a small typo in parameter name but I've edited it so it should work now

@captainsameer use the edited version which renames a to numbers and it will be solved.

Simpler is to drop the map function and just use sum(i*i for i in numbers if i%2 == 0)

@PaulCornelius True, don't know why I haven't thought about that in a first place.
