Python Forum

Full Version: merging lists, dedup and keeping order, in older pythons
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
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 :)
so, sets do not use the same logic as dictionaries use for their keys?
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/m...etobject.c