2
n1, n2, n3 = (n + [None]*2)[:3]

I just very briefly want to know what this does, how this works. Is this like a list comprehension, as long as I offer a list or iterable with enough variables will it assign the n1 = iterable[0]?

Also why does having [:3] on the end of the brackets limit the length?

I understand why [None] is there, just in case the length of n is less than 3 but can someone give me a bit more info?

I have only been coding for about a week and need a bit of help interpreting.

asked Oct 2, 2015 at 9:52
1
  • FWIW, n should be a list (or a suitably-defined custom object). It can't be just any iterable. Eg, if n is a tuple you'll get TypeError: can only concatenate tuple (not "list") to tuple. Similarly, it won't work if n is a string. Commented Oct 2, 2015 at 10:31

2 Answers 2

7

This takes the first three elements of n, padding with None if it has fewer than three elements.

(n + [None]*2) concatenates a list of [None, None] to the list n, and as you say, [:3] takes the first three items of the resulting list. These three items are then unpacked to the variables n1, n2 and n3.

For example:

In [1]: n = ['One', 'Two']
In [2]: n1, n2, n3 = (n + [None]*2)[:3]
In [3]: print n1
One
In [4]: print n2
Two
In [5]: print n3
None

But if n has three or more items in it, you just get those three items as n1, n2 and n3.

As others have noted below, this code will fail if n is the empty list, since then (n + [None]*2) will only have two items in it. It can be sliced OK: (n + [None]*2)[:3] returns a list with those two items, but then the unpacking to three variables fails. Whether the resulting ValueError in this case is the intended exception to be raised or whether the code should be n1, n2, n3 = (n + [None]*3)[:3] so as to return None into each of the variables is something only the original programmer knows. This would usually warrant a comment in the code.

answered Oct 2, 2015 at 9:54
Sign up to request clarification or add additional context in comments.

9 Comments

n should be list. It would be nice if you mention this though
True, that: I've expanded my answer a bit. Though n isn't really a great choice of name for a list IMO.
Its possibly worth mentioning that if n is the empty list then (n + [None]*2)[:3] will succeed,resulting in [None, None], but then the unpacking to the tuple n1, n2, n3 will fail.
@xnx No need to be humble about your opinion: n is a lousy name for a list, especially when it's used with the overloaded + operator, and especially as a name for a list of things whose elements are named by n1, n2, n3, . All of these names are conventionally used for integers.
As @PM 2Ring mentions, the statement fails if n is empty. Probably [None]*3 should be have been used - that would be safe.
|
3

This is not list comprehension. Presumably, n is a list, or anything that can be the left-operand of concatenation by the + operator when called with the suitable right-operand, under suitable implementation of __add__ or __radd__. The + operator on lists concatenates them together to create a new list, and the [:3] part is just syntax sugar for getting a slice of the list. You really should look up the documentation.

answered Oct 2, 2015 at 9:56

4 Comments

I know what you mean, however, the result of n + [None]*2 isn't necessarily concatenation: if n is an instance of a class with an appropriate __add__ method, then n + some_list can return anything the class's implementer wants it to.
Well, it's not as general as all that: n can be anything that be a first operand of the + operator when the second operand is a list -- here, [None]*2. It can't be a tuple, for example (raises TypeError).
@PM2Ring I'm not implying that it is necessarily concatenation.
@BrianO acknowledged.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.