2

I want to ask if something like this is possible in python:

a,b = [i,i+1 for i in range(5)]

I know this isn't possible because I have got an error, but I think you understand what I am trying to achieve. Let me clear it up, I can do :

a,b = 3+2,3

Edit ---> Or even better:

a,b = [0,1,2,3,4],[1,2,3,4,5]

I wan't a similar thing in my first code example. I am trying to assign variables 'a' and 'b' as list, with list comprehension, but using tuple as assignment, the point is I don't want to use this:

a = [i for in range(5)]
b = [i+1 for in range(5)]

I am aware that I can use this: t = [(i,i+1) for i in range(5)], but that's not the point.
By the way this is only a simple example => "i,i+1"

Edit ---> I would like to clarify my question. How to assign several variables (type list) in one line, using list comprehension?

3
  • Don't ask hypothetical questions. Your real problem might very well quite a different solution than your simplified one.
    – user3850
    Feb 1, 2014 at 3:31
  • See X/Y problem
    – mhlester
    Feb 1, 2014 at 3:34
  • @hop: That's just a bad argument! The question is obvious : How to assign several variables using one loop and list comprehension.
    – Stevan
    Feb 1, 2014 at 3:42

2 Answers 2

7

When you run this:

a,b = [(i,i+1) for i in range(5)] # wrapped i, i+1 in parentheses (syntax error)

It makes a list of five two-item tuples, like this:

[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]

But you're trying to assign those five tuples to only two objects (a and b)

Using argument unpacking (*) in zip, you can "unzip" the output to the first and second elements of each tuple:

a,b = zip(*[(i,i+1) for i in range(5)])

Which is this:

[(0, 1, 2, 3, 4), (1, 2, 3, 4, 5)]

And can be assigned to a and b as you've written

5
  • That's nice solution, thanks. I just wanna wait and see, if someone posts any better, until i accept the answer.
    – Stevan
    Feb 1, 2014 at 3:16
  • Didn't pay attention to the unzip option, zip(*..). Thanks for pointing that out.
    – Stevan
    Feb 1, 2014 at 3:19
  • @hop, I fully agree, but OP did say "this is only simple example => "i,i+1"" so I assume there's more to it than we've seen
    – mhlester
    Feb 1, 2014 at 3:28
  • @mhlester: well then the question is worthless, isn't it?
    – user3850
    Feb 1, 2014 at 3:30
  • 1
    @hop the question is how to unpack a list comprehension
    – mhlester
    Feb 1, 2014 at 3:31
1

Don't try to be clever. This is perfectly acceptable code:

>>> a = range(5)
>>> b = range(1,6)
>>> a, b
([0, 1, 2, 3, 4], [1, 2, 3, 4, 5])
2
  • I can say same for you too. My line : " By the way this is only simple example => "i,i+1" ". The point of this question is using only one loop and using list comprehension assign several variables.
    – Stevan
    Feb 1, 2014 at 3:31
  • :A simple one, indeed.
    – Stevan
    Feb 1, 2014 at 3:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.