fastest way to merge lists.
Hrvoje Niksic
hniksic at iskon.hr
Sat Jan 8 22:19:37 EST 2000
Roy Smith <roy at popmail.med.nyu.edu> writes:
> I'm not yet sure if care if the final list is sorted or not. For
> now, I think it doesn't matter. What's the fastest way to do that?
> It seems to me:
>
> d = {}
> for s in list1:
> d[s] = 1
> for s in list2:
> d[s] = 1
> for s in list3:
> d[s] = 1
> for s in list4:
> d[s] = 1
> for s in list5:
> d[s] = 1
> list = d.keys()
> del d # if I care about memory
>
> would be pretty good. Anything better?
How about:
# cat
biglist = list1 + list2 + list3 + list4 + list5
# sort
biglist.sort()
# uniq
index = 0
while index < len(biglist) - 1:
if biglist[index] == biglist[index + 1]:
del biglist[index]
else:
index = index + 1
I haven't compared this solution with yours, but note that it's
directly equivalent to Unix-ese `cat | sort | uniq'.
More information about the Python-list
mailing list