I am trying to learn python, especially lists, and I have a small problem. I have two lists:
alist=[1,2,3,4,5,6,7,8,9,1,2,3]
blist=["a","b","c","d","e","f","g","h","i","a","b","c"]
what I’s like to do is remove duplicates from “both the lists”. i.e only when an element of alist and its corresponding element in blist (in pairs) are the same, I delete them. I am expecting an outcome, looking like:
alist=[1,2,3,4,5,6,7,8,9]
blist=["a","b","c","d","e","f","g","h","i"]
So, I try to first do a set on the zip:
kk=set(zip(alist,blist))
and then do:
alist[:],blist[:]=zip(*kk)
then I get:
alist=[5, 6, 4, 7, 3, 8, 2, 1, 9]
blist=['e', 'f', 'd', 'g', 'c', 'h', 'b', 'a', 'i']
as you can see, the order is not maintained. I was wondering if it was possible somehow to use “sorted” and “index” to get the lists in correct order. Any guidance would be much appreciated.
The python version is 2.7.3
You could use
collections.OrderedDictfor this:An ordered set — if it were available — would perhaps lead to an even cleaner solution.