Python 2.7: Add 2 list items in a function
<p>I want to be able to add multiple items to my code with a function. But I want to be able to still add one single item, or none. Here is and example.</p>

<pre><code>listX = ["a", "b", "c"]

def addList(add):
    if add != "null":
        listX.append(add)

addList("d")
</code></pre>

<p>Adds d at the end. Simple.</p>

<pre><code>print listX

addList("e" + "f")

print listX
</code></pre>

<p>Now it's ['a', 'b', 'c', 'd', 'ef']. <strong>I want it to be ['a', 'b', 'c', 'd', 'e', f].</strong> Also, I also want to have only one argument for adding an item the (add) one.</p>

<p>How do I do this? Please help. And as always, thanks in advance.</p>

<p>Your Function <code>addList</code> expects only 1 argument, when you say</p>

<pre><code>addList("e" + "f") # it passes it as addList("ef")
</code></pre>

<p>To solve your problem simply use <code>*</code> to get multiple arguments, and <code>extend</code> to add to it easily:</p>

<pre><code>listX = ['a', 'b', 'c']

def addList(*args):
  listX.extend(args)

addList('d', 'e', 'f')
print (listX)
</code></pre>

<p>returns:</p>

<pre><code>['a', 'b', 'c', 'd', 'e', 'f']
</code></pre>

python python-2.7 function variables