-1

How do I write both short and long names in a single list comprehension line?

#this is how i tried below code but its not working.
short_names, long_names = [(i,j) for i,j in planet_names if len(i) <= 5  
else len(j) > 5 ]

#working code
planet_names = ["Mercury", "Venus", "Earth", "Mars", "Jupiter","Saturn"]                         
short_names = [i for i in planet_names if len(i) <= 5]
long_names = [i for i in planet_names if len(i) > 5]
Sai Kumar
  • 606
  • 2
  • 7
  • 21
  • 2
    If you want to build up two separate lists, you almost always just want either two list comprehensions, or an explicit `for` loop statement. – abarnert Jul 20 '18 at 19:44
  • What you ask for is not supported in Python. List comprehensions return a single list. Additionally, the semantic of `else` is to provide another value or expression for the same element, but not to populate a different list. – Luis Masuelli Jul 20 '18 at 19:44
  • sorry i dont know much about itertools, would prefer the list comprehension – Sai Kumar Jul 20 '18 at 19:44
  • Is there any reason why you want to do it with comprehension? This is a good candidate for a normal for loop. You declare 2 empty lists and then you can iterate once over your planet list appending the planet name to the right list basing on str lenght – Mattia Procopio Jul 20 '18 at 19:45
  • Your approach is fine. Are you just looking for a one-liner? If so, I have a solution using `functools.reduce()` but it's not a list comprehension and I think the [answer](https://stackoverflow.com/a/51449412/5858851) by below @mad_ is even better – pault Jul 20 '18 at 19:49
  • i wanted to know more about list comprehension and see if i could populate both the list from a single list comprehension. anyways Thanks though! – Sai Kumar Jul 20 '18 at 19:50
  • @pault can you post your solution down below – Sai Kumar Jul 20 '18 at 19:53
  • a solution is this: short=[] long=[] [short.append(x) if len(x) < 5 else long.append(x) for x in planet_names] I cant post as answer because marked as duplicated... – Joe Jul 20 '18 at 19:57
  • Don't recommend doing this, but since you asked: `short_names, long_names = reduce(lambda a, b: (a[0]+[b],a[1]) if len(b) <=5 else (a[0], a[1]+[b]), planet_names, ([],[]))`. To reiterate, this is **bad** for many reasons. – pault Jul 20 '18 at 20:04
  • `t1, t2 = itertools.tee(planet_names); short_names, long_names = map(list, (filter((lambda x: len(x)<=5), t1), filter((lambda x: len(x)>5), t2)))` – abarnert Jul 20 '18 at 20:25
  • If you want to understand what that does, and how to wrap it up in a more comprehensible one-liner, and why you usually don't want to do it, I added [an answer](https://stackoverflow.com/a/51449836/908494) to the duplicate question. But really, you just want two listcomps or Martijn's answer here. – abarnert Jul 20 '18 at 20:26

1 Answers1

3
short_names=[]
long_names=[]
planet_names = ["Mercury", "Venus", "Earth", "Mars", "Jupiter","Saturn"]                         
for i in planet_names :
    if len(i) <= 5:
        short_names.append(i) 
    else:
        long_names.append(i)
mad_
  • 7,844
  • 2
  • 22
  • 37