Python Forum
merging lists, dedup and keeping order, in older pythons - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: merging lists, dedup and keeping order, in older pythons (/thread-13345.html)



merging lists, dedup and keeping order, in older pythons - Skaperen - Oct-11-2018

i want to merge several (large) lists and remove duplicates. a set seems useful because all items are strings that can be saved in a set. but i also want to retain order an run on older versions of python. still, it seems not that hard (copy items while checking a set for dupes). but, before i code that, i am wondering if python has such a thing, already. it would seem silly to code it if it is already existing. yet, i am not good at finding builtin functions in the docs.

i know that dictionaries in 3.7 maintain the order added. but this code will need to be able to run in earlier versions, too.

i'm curious about that feature of dictionaries in 3.7. do sets have the same feature? and if so, is that order carried along when making s frozen set from a set or list or tuplr?


RE: merging lists, dedup and keeping order, in older pythons - ODIS - Oct-11-2018

Sets don't keep order and python doesn't include built-in ordered sets. But you can use ordered dictionary from the collections package which is also in Python 2:

from collections import OrderedDict

list1 = ["one", "two"]
list2 = ["three", "four"]

merge = OrderedDict()

for a_list in [list1, list2]:
    for item in a_list:
        merge[item] = None

print(merge.keys())
Sets stay unordered in Python 3.7 :)


RE: merging lists, dedup and keeping order, in older pythons - Skaperen - Oct-13-2018

so, sets do not use the same logic as dictionaries use for their keys?


RE: merging lists, dedup and keeping order, in older pythons - ODIS - Oct-19-2018

I think they do (some hashtable data structure probably). But ordering is in Python 3.7 implemented only for the the dictionaries.

You can check out the source code: https://github.com/python/cpython/blob/master/Objects/setobject.c