0
FirstList = [10, 20, 23, 11, 17]
SecondList = [13, 43, 24, 36, 12]

thirdlist = [num for num in FirstList if num%2==1]
thirdlist.extend([num for num in SecondList if num%2==0])
print(thirdlist)

So my question is how can i erase thirdlist.extend() and compile in one line? My approach was

thirdlist=[num for num in FirstList if num%2==1 , num1 for num1 in Second ...] 

Desired output for above example:

[23, 11, 17, 24, 36, 12]'
Keyur Potdar
  • 7,158
  • 6
  • 25
  • 40
Ata Reenes
  • 188
  • 1
  • 10
  • in this case '[23, 11, 17, 24, 36, 12]' – Ata Reenes Jan 03 '21 at 14:20
  • `[a for a in FirstList if a%2==1] + [a for a in SecondList if a%2==0]` – warped Jan 03 '21 at 14:21
  • 1
    You *could* use `[*(genexpr), *(genexpr)]` but your going to end up wanting to put it on multiple lines anyway. There's nothing wrong with your approach – juanpa.arrivillaga Jan 03 '21 at 14:22
  • i was thinking about genexpr too but i wanted to know if there is a way to sum them in one line – Ata Reenes Jan 03 '21 at 14:25
  • I just gave you one way. Concatenating the results of the list comprehension is another way as mentioned above – juanpa.arrivillaga Jan 03 '21 at 14:27
  • It might be too obvious, but lists can be added to each other. So rewriting the `thirdlist.exend([])` with `thirdlist + []` is doable. Doing this for the first list comprehension as well, would result in a single line like `result = [] + []` – albert Jan 03 '21 at 14:31

3 Answers3

2

One way to do it using + both of the list comprehension in one line,

FirstList = [10, 20, 23, 11, 17]
SecondList = [13, 43, 24, 36, 12]
thirdlist = [num for num in FirstList if num%2==1] + [num for num in SecondList if num%2==0]
print(thirdlist)
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
0

You can use itertools.chain instead of +:

>>> from itertools import chain

>>> list(chain([num for num in FirstList if num%2==1], [num for num in SecondList if num%2==0]))
[23, 11, 17, 24, 36, 12]
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
0

A list comprehension is, in some sense, a syntactic wrapper around a generator expression. In this case, I would explicitly use generator expressions, and chain them together with itertools.chain. This approach has the advantage of creating one list object, not two temporary objects that are used to create the third, final object.

from itertools import chain


FirstList = [10, 20, 23, 11, 17]
SecondList = [13, 43, 24, 36, 12]

thirdList = list(chain((num for num in FirstList if num % 2 == 1),
                       (num for num in SecondList if num % 2 == 0)))
print(thirdlist)

You might also want to use filter instead of a generator expression for readability.

thirdList = list(chain(filter(lambda x: x%2 == 1, FirstList),
                       filter(lambda x: x%2 == 0, SecondList))

or

def even(x):
    return x % 2 == 0

def odd(x):
    return x % 2 == 1

thirdList = list(chain(filter(even, FirstList), filter(odd, SecondList)))
chepner
  • 497,756
  • 71
  • 530
  • 681