1

Sorry if this is a duplicate question, I searched and couldn't find anything to help.

I'm currently trying to compare two lists. If there are any matching items I will remove them all from one of the lists.

However the results I have are buggy. Here is a rough but accurate representation of the method I'm using:

>>> i = [1,2,3,4,5,6,7,8,9]
>>> a = i
>>> c = a
>>> for b in c:
    if b in i:
        a.remove(b)


>>> a
[2, 4, 6, 8]
>>> c
[2, 4, 6, 8]

So I realised that the main issue is that as I remove items it shortens the list, so Python then skips over the intermediate item (seriously annoying). As a result I made a third list to act as an intermediate that can be looped over.

What really baffles me is that this list seems to change also even when I haven't directly asked it to!

NDevox
  • 4,056
  • 4
  • 21
  • 36
  • 1
    `a = i[:]` - you need to copy, not make a new reference to the same object. – g.d.d.c Apr 11 '14 at 20:04
  • possible duplicate of [How do you remove duplicates from a list in Python whilst preserving order?](http://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-in-python-whilst-preserving-order) – Steinar Lima Apr 11 '14 at 20:07

4 Answers4

2

In python, when you write this:

i = [1,2,3,4,5,6,7,8,9]

You create an Object (in this case, a list) and you assign it to the name i. Your next line, a = i, tells the interpreter that the name a refers to the same Object. If you want them to be separate Object you need to copy the original list. You can do that via the slicing shorthand, i[:], or you can use a = list(i) to be more explicit.

g.d.d.c
  • 46,865
  • 9
  • 101
  • 111
1

The easiest way to do this is use a set to determine shared items in a and b:

for x in set(a).intersection(b):
    a.remove(x)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • Thanks! I didn't realise Python had an intersection method. I'm going through these methods now which I should have found ages ago. – NDevox Apr 11 '14 at 20:12
  • I've got a big Python book that I got specifically to try and understand more of the fundamentals and standard types of Python. But I've got a plans which are already set in motion and it's easier to learn by doing then it is to learn by reading! It has made me a bit reliant on stack overflow for simple things like this though. – NDevox Apr 11 '14 at 20:17
  • 1
    Worth bookmarking standard [functions](https://docs.python.org/2/library/functions.html) and [types](https://docs.python.org/2/library/stdtypes.html). – jonrsharpe Apr 11 '14 at 20:19
1

a = i Doesn't make a copy of a list, it just sets another variable, i to point at your list a. Try something like this:

>>> i = [1, 2, 3, 2, 5, 6]
>>> s = []
>>> for i in t:
       if i not in s:
          s.append(i)
>>> s
[1, 2, 3, 5, 6]

You can also use set which guarantees no duplicates, but doesn't preserve the order:

list(set(i))
Tyler
  • 17,669
  • 10
  • 51
  • 89
  • Thanks. I've been using set previously to get rid of duplicates but these need to remain as two distinct lists to be able to work for the rest of my programme. Fixed now though! – NDevox Apr 11 '14 at 20:11
1

Your statements a = i and c = a merely make new names that reference the same object. Then as you removed things from a, it's removed from b and i, since they are the same object. You'll want to make copies of the lists instead, like so

a = i[:]
c = a[:]
CDspace
  • 2,639
  • 18
  • 30
  • 36