0

How can I initialize a list, if it is not already initialized and append to it in one line. For example,

function example(a=None):
   a = a or []
   a.append(1)
   return another_function(a)

How can I combine those two statements in the function into one?

I am looking for something like this, but that does not work:

function example(a):
   a = (a or []).append(1)
   return another_function(a)

EDIT: I don't reference a elsewhere, it is just being passed from function to function. Practically just value is important, so it is OK if it is another object with the right value. I also added a default value of None.

mehmet
  • 7,720
  • 5
  • 42
  • 48
  • 3
    Why not just use two lines? – user2357112 Oct 07 '16 at 19:43
  • 3
    How would it ever not be initialized? What other value would it have instead? – Morgan Thrapp Oct 07 '16 at 19:43
  • this parameter a is being passed between several such functions and any one of them might initialize. – mehmet Oct 07 '16 at 19:45
  • As @MorganThrapp says, it's not clear what else it could be. You would have to send something to that function. It's either a list and will work, or it isn't and won't. – TigerhawkT3 Oct 07 '16 at 19:45
  • @TigerhawkT3 could it not be None? – mehmet Oct 07 '16 at 19:51
  • And is the list supposed to be used only within the function (you rebind the name to a local reference, with the same object or a different one)? Or is it supposed to mutate the original list? Or return a new one? – TigerhawkT3 Oct 07 '16 at 19:51
  • You should generally check for `None` explicitly, to differentiate between `None` and another falsey object like an empty list. If you have a list reference that you'd like to preserve, passing a reference to an empty list will be replaced with a new empty list object. Your use case isn't clear. – TigerhawkT3 Oct 07 '16 at 19:53
  • I used `a` within scope of several functions and don't reference it elsewhere. Therefore, it is like passing by value. I edited the question to clarify. – mehmet Oct 07 '16 at 19:57
  • 1
    The reason your attempt isn't working is that `a.append(x)` mutates the list object in place and returns `None`. That is a pattern you should assume in Python. Read [Alex Martelli's answer here](http://stackoverflow.com/questions/1682567/why-does-pythons-list-append-evaluate-to-false). Also, although `x or y` works, it perhaps more common in modern Python to use the conditional expression `x if x else y` – juanpa.arrivillaga Oct 07 '16 at 20:04
  • @user2357112 you can call it OCD (obsessive compulsive disorder). I could do it with object properties in javascript and found it very elegant.. That is why. – mehmet Oct 07 '16 at 20:18

1 Answers1

3
def example(a):
    return a + [1] if a else [1]

>>> example([1,2,3])
[1, 2, 3, 1]
>>> example([])
[1]
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • 5
    This doesn't exactly _append_ to a list. I creates a new list that has the last item added. There's a slight difference in some cases... – mgilson Oct 07 '16 at 19:44
  • 2
    OP actually said that the same list object could be initialized by another function, implying that the reference should be preserved. I don't think this is the answer – salezica Oct 07 '16 at 19:49
  • @slezica I said "parameter a is being passed between serveral such functions and any one of them might initialize". I did not say "same object". Until it is initialized I don't care. – mehmet Oct 07 '16 at 20:00
  • On another note, what a vibrant python community is here! If I knew I would be much more deliberate when asking the question. I would ask about say 'django' and wait days to get an answer. – mehmet Oct 07 '16 at 20:01