Avatar ·

Element-wise addition of lists of different lengths

📁 Advanced Python Topics

There are two lists:

a = [1,2,3]b = [4,5,6,7,8]

Need to get this result:

a + b = [5,7,9,7,8]

I hope the idea is clear. I only have hacky solutions in mind, I'd like to find a more or less adequate solution.

Avatar ·

There is a brute-force solution

example = models.Object.objects.values('name', 'photo__name').extra(    where=['""name"" != ""photo__name""']).distinct()

which generates approximately the following SQL

SELECT ""object"".""name"", ""photo"".""photo__name"" FROM ""object"" INNER JOIN [...]  WHERE (""name"" != ""photo__name"") ``

possibly due to the join you'll need to specify full table names, for example

where=['""object""                                

Log in to leave an answer

Blogs