0

We have the following list as input:

[[-5, -1], [1, -5], [5, -1]]

I have created a function which takes clist as a list and number a random number that I want to remove from the list.

The function should remove all the nested lists that contain the given number and remove the negative number within the nested list

def reduce(self, clist, number):
    self.temp = list(clist)

    # remove the nested list that contain the given number
    for item in clist:
        if number in item:
            self.temp.remove(item)

    # remove the negative number within the nested list
    for obj in self.temp:
        try:
            obj.remove(-number)
        except:
            pass

    return self.temp

Let's pick 1 as a number and run the code.

  • The first for loop will remove all nested lists that contain the given number and will get the following:

    self.temp = [[-5, -1], [5, -1]]

    clist = [[-5, -1], [1, -5], [5, -1]]

  • The second for loop should remove all negative number within the nested lists but we get the following:

    self.temp = [[-5], [5]]

    clist = [[-5], [1, -5], [5]]

What I do not get is why clist gets affected when I am working on the second for loop, especially when I'm working on self.temp list? It should be without reference to the original list, but I am missing something out. Help?

2
  • can you show the complete code for this Commented Sep 19, 2019 at 14:06
  • @hsnsd Unfortunately no because it's part of my assignment. But I can tell you other bits that might influence, but so far this is that part that influences my result. Commented Sep 19, 2019 at 14:17

1 Answer 1

1

Seems like a nested list comprehension would be easiest:

def reduce(clist, number):
    return [[x for x in subl if x != -number] for subl in clist if number not in subl]

print(reduce([[-5, -1], [1, -5], [5, -1]], 1))
# [[-5], [5]]

That iterates twice over lists that don't contain number, so a slightly more efficient solution would be, though actual speed will depend on your data.

def reduce(clist, number):
    result = []
    for subl in clist:
        temp = []
        for x in subl:
            if x == number:
                break
            elif x != -number:
                temp.append(x)
        else:
            result.append(temp)  # Only happens if there was no break
    return result

You can save this result to self.temp if you want (after adding self back to the parameters), but it wasn't clear to me if your actual intention was to save the result to the object or not.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that worked out. It's not necessary to save the result. The one thing I didn't understood is that why when removing an element from self.temp from the 2nd for loop, it affects the clist as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.