-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
  • 665
  • 2
  • 9
  • 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_
  • 8,121
  • 2
  • 25
  • 40
  • This is not a list comprehension, as the OP requested it to be. – Thierry Lathuille Jul 20 '18 at 19:45
  • Direct and to the point. – gahooa Jul 20 '18 at 19:45
  • @ThierryLathuille what OP asked for is not possible. – Karl Knechtel Jan 30 '23 at 05:05
  • @KarlKnechtel It is, the list comp just needs to build a list containing the two desired lists. – Kelly Bundy Jan 30 '23 at 05:10
  • I mean, pedantically, I suppose `[[i for i in planet_names if condition(i)] for condition in [(lambda x: len(x) <= 5), (lambda x: len(x) > 5)]]` would qualify. But we answer *practical* questions here. The question is clearly a duplicate anyway. – Karl Knechtel Jan 30 '23 at 05:13
  • @KarlKnechtel Not even that far-fetched, as I independently wrote almost identical [code](https://tio.run/##dY6xCsIwFEX3fMUjUwvZpCBi3VyEToJLCRI1tSnpS3hNhn59JEWQIr3T5XLgXD@H3uFu7yklbxXqcEc16glqaHmj6Rlp5gL4TWOccjkrCn0ujaJluERvgiYu@FWFSMglbIWxqXf0NQiwDt8/G8tEa6BzBAYMwuqO6cAXppQLlRGfkdaq8fFSMBzAaiyGEo41VIKttX/QCSrJJGOeDIZi41OZ0gc) :-) – Kelly Bundy Jan 30 '23 at 05:15