Python Forum
merging lists, dedup and keeping order, in older pythons
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
merging lists, dedup and keeping order, in older pythons
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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 :)
Reply
#3
so, sets do not use the same logic as dictionaries use for their keys?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Installing Older Version Of Numpy & Pandas knight2000 6 1,785 Aug-30-2023, 10:58 AM
Last Post: knight2000
  Keeping up with IDEs and Virtual Environments... bytecrunch 7 2,506 Sep-05-2022, 08:04 PM
Last Post: snippsat
  Keeping a value the same despite changing the variable it was equated to TheTypicalDoge 2 1,461 Mar-13-2022, 10:50 PM
Last Post: Yoriz
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,805 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  coding problem from older book teddfirth 3 2,147 Mar-06-2021, 03:51 PM
Last Post: teddfirth
  how to pass arguments between pythons scripts? electricDesire 2 2,162 Oct-19-2020, 07:19 PM
Last Post: electricDesire
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,372 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Problem keeping the window up benlyboy 11 4,143 Jan-24-2020, 02:12 AM
Last Post: tamilselvisubbiah
  remove files from folder older than X days kerzol81 2 8,650 Jan-03-2020, 11:55 PM
Last Post: snippsat
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,274 Mar-20-2019, 08:01 PM
Last Post: stillsen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020