Confusion about Python Functions and Lists
<p>I am trying to create a function to remove an item from the passed list either by a specified index, or item passed. </p>

<p>If the user wishes to remove an item from the list using an index, the third argument passed will be <code>âindexâ</code>, if the user wishes to remove the first item found in the list using the item passed, the second argument will be <code>â{item}â</code> </p>

<p>For example, to remove the item at index 3 from a list, this would be the command <code>myFunction(myList,3,âindexâ)</code></p>

<p>I am quite confused about this function part.  I have written code that does exactly what the question seems to ask, but it does not use a function.  My code is below :</p>

<pre><code>mylist = ["one" , "two" ,"three" , "four" , "five"]
print "list is composed of: "+ str(mylist)
name = raw_input("Index of item to be removed. ex.  1")
name2 = raw_input('"item to be removed. ex. four')
name3 = int(name)
del mylist[name3]
mylist.remove(name2)
print mylist
</code></pre>

<p>It appears that I need to create a function to do this, and then pass in my list, the index/item, etc.)  but I am very lost on this part. </p>

<p>The question (I think) is:   "<em>If the user wishes to remove an item from the list using an index, the third argument passed will be âindexâ, if the user wishes to remove the first item found in the list using the item passed, the second argument will be â{item}â</em>"</p>

<p>The purpose of this exercise (presumably) is to practice writing a function.  Yes you could do it without a function but right now you need the practice of writing a function and passing parameters.  Functions are a very important part of programming, but this is not a good place to go into that.</p>

<p>So first we define our function:</p>

<pre><code>def removeItem( theList, theItem, typeOfItem=None ):
</code></pre>

<p>Notice I have given a default value of <code>None</code> because the third parameter is optional.</p>

<p>The first thing we will do is to test <code>typeOfItem</code>.  The question says that is it is an index then it will say <code>"index"</code> else the second parameter will say <code>"{item}"</code>.  So it will be one or the other.  (What to do if that is not the case is a question you should ask).</p>

<p>The index part is easy:</p>

<pre><code>    if typeOfItem == "index":
        del(theList[theItem])
</code></pre>

<p>but now its a bit more complicated, because of the <code>{  }</code>, which we have to remove:</p>

<pre><code>    else:
        theList.remove(theItem[1:-1])
</code></pre>

<p>This last part is removing a <em>slice</em>, which starts at character 1 (the second character) and ends at the final character -1, thus removing the <code>{ }</code> </p>

<p>So the final function code, with tests, is:</p>

<pre><code>def removeItem( theList, theItem, typeOfItem=None ):
    if typeOfItem == "index":
        del(theList[theItem])
    else:
        theList.remove(theItem[1:-1])

mylist = ["one" , "two" ,"three" , "four" , "five"]
removeItem(mylist, 3, "index")
print mylist

mylist = ["one" , "two" ,"three" , "four" , "five"]
removeItem(mylist, "{two}")
print mylist
</code></pre>

<p>Notice an important feature of the function and the list.  If you alter the list inside the function then it also alters it <em>outside</em> the function as well - it is the same list.  That is not the case with numbers and strings.</p>

python