ed.hank ed.hank - 2 months ago 7
Python Question

Confusion about Python Functions and Lists

I am trying to create a function to remove an item from the passed list either by a specified index, or item passed.

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}”


For example, to remove the item at index 3 from a list, this would be the command
myFunction(myList,3,”index”)


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 :

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


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.

Answer

The question (I think) is: "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}”"

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.

So first we define our function:

def removeItem( theList, theItem, typeOfItem=None ):

Notice I have given a default value of None because the third parameter is optional.

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

The index part is easy:

    if typeOfItem == "index":
        del(theList[theItem])

but now its a bit more complicated, because of the { }, which we have to remove:

    else:
        theList.remove(theItem[1:-1])

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

So the final function code, with tests, is:

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

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

Comments