Python Forum
list approach due nested order
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list approach due nested order
#1
Horrible thread name, but let me try and explain it.

I put lists (with 2 (always) values) in a main-list and I want to cleanup the duplicates in that main-list. The order of the 2 values don't matter to me and are therefor the same.

mainlist = []
listA = ["a", "b"]
listB = ["b", "a"]
listC = [1,2]

mainlist = [ listA, listB ]
unique-mainlist = [ ["a", "b"], [1,2] ]
Reply
#2
Since the order doesn't matter, you might want to consider using a set of frozensets:
>>> unique = set()
>>> unique.add(frozenset(['a', 'b']))
>>> unique.add(frozenset(['b', 'a']))
>>> unique.add(frozenset([1, 2]))
>>> unique
{frozenset({'a', 'b'}), frozenset({1, 2})}
Reply
#3
frozenset... awesome. New functionality for me, but your suggestion certainly works Thanks!.

Tiny side question:
from a pythonic point of view is it better to
- check if a list (or reversed) exists in main-list
- if so, skip, else add

Or like my current approach:
- dump all in main-list
- frozenset them

Thinking in large numbers here.
Reply
#4
Combining frozenset with unique_everseen yields my preferred solution
from more_itertools import unique_everseen

def create():
    yield ['a', 'b']
    yield ['b', 'a']
    yield [1, 2]
    
unique = list(unique_everseen(create(), key=frozenset))
print(unique)
Output:
[['a', 'b'], [1, 2]]
Reply
#5
Hmmm, more_itertools isn't installed.

Unsure if I can install this.
Reply
#6
3Pinter Wrote:Unsure if I can install this.
Try
Output:
python -m pip install more_itertools
Reply
#7
# mainlist is a massive list 

joinlist = []
unique = set()
for m in mainlist:
	unique.add(frozenset(m))

for u in unique:
	joinlist.append(list(u))

if joinlist:
	for join_pair in joinlist:
		#do something
		joinelements(join_pair)
This is working, but feels a bit clumpsy. And looking at the yield functionality ... I think my coding could be improved?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Advice needed on how to approach this problem... sawtooth500 1 243 Apr-06-2024, 01:55 PM
Last Post: snippsat
  Copying the order of another list with identical values gohanhango 7 1,133 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,316 May-22-2023, 10:39 PM
Last Post: ICanIBB
  List all possibilities of a nested-list by flattened lists sparkt 1 911 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Updating nested dict list keys tbaror 2 1,271 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Python Program to Find the Total Sum of a Nested List vlearner 8 4,886 Jan-23-2022, 07:20 PM
Last Post: menator01
  Looping through nested elements and updating the original list Alex_James 3 2,111 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  Order a list with successive permutations based on another list yvrob 3 2,787 Mar-19-2021, 08:20 AM
Last Post: supuflounder
  Group List Elements according to the Input with the order of binary combination quest_ 19 6,401 Jan-28-2021, 03:36 AM
Last Post: bowlofred
  Sound Approach for Running a Shell Command? matt_the_hall 8 3,330 Dec-14-2020, 02:52 PM
Last Post: matt_the_hall

Forum Jump:

User Panel Messages

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