0

I need some help please, I have some code I've written which should manipulate a list using a function. The problem I'm having is keeping hold of this manipulated list to use in the main coding. For example, I created a list which contains names for 4 people queuing in line for tickets, I then input this list in a function to remove the 3rd in line. This will create a new list which I want to be able to manipulate outside of the function. Here is my code so far

def aLeave(aList,usrstr):

    tempq = []
    idx = 0
    found = False
    while idx < len(aList) and not found: #This section works out the index
        if aList[idx] == usrstr:          # of the user string that needs removed
            found = True                  # from the queue list.
        else:
            idx = idx + 1

    if found:        
        for i in range(len(aList)):           #This sections takes the index previously 
            if i == idx:                      #found and uses it to create a new list
                continue                      #without the element the user has requested to be removed
            tempq.append(aList[i])

    aList = tempq
    print(aList)
    return aList

aList = ["john","mark","pete","dave"]

aLeave(aList,input("what do you want to remove"))

print (aList)

Any help would be very much appreciated!

Thanks (The function is called 'aLeave')

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • `aList` is a *local variable* inside your function. Assigning to it won't modify the global `aList`. You could use `global aList` at the top of your function, but that is bad design. Rather, you should just `return` the list, as you have. But then, you have to do something like `aList = aLeave(aList, input('...'))` or else the return value isn't assigned to anything, is no longere referenced, and gets garbage collected. You also could just `return tempq` and leave out `aList = tempq` in your function. – juanpa.arrivillaga Mar 28 '17 at 21:34
  • Also, you really don't need to use two loops to accomplish what you are doing. Just `for x in aList: if x != usrstr: tempq.append(x)` – juanpa.arrivillaga Mar 28 '17 at 21:38
  • Thanks @juanpa.arrivillaga I realised it could be done simpler after I wrote the code. I will try your suggestions. – kingpete84 Mar 28 '17 at 22:29

1 Answers1

3

You just have to assign the return value of the function to a variable, and since you want to update the same list, you can do something like this while calling the function.

aList = aLeave(aList, input("Stuff"))

Also, inside the function, aList = tempq is not necessary, since all it is doing is updating the local variable, aList. To update the aList in the global scope, you can write global aList at the top of the function, however, this is not considered good design practice, and should be avoided as far as possible.

  • Thanks @Shubham Jindal. I had extra lines of code in to try and make it work. Have slimmed it down and managed to return the new list also. :) – kingpete84 Mar 28 '17 at 22:37