Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6749791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:43:56+00:00 2026-05-26T12:43:56+00:00

I have a datastructure like this: [ [(‘A’, ‘1’), (‘B’, ‘2’)], [(‘A’, ‘1’), (‘B’,

  • 0

I have a datastructure like this:

[
[('A', '1'), ('B', '2')],
[('A', '1'), ('B', '2')],
[('A', '4'), ('C', '5')]
]

And I want to obtain this:

[
[('A', '1'), ('B', '2')],
[('A', '4'), ('C', '5')]
]

Is there a good way of doing this while preserving order as shown?

Commands for copy-pasting:

sample = []
sample.append([('A', '1'), ('B', '2')])
sample.append([('A', '1'), ('B', '2')])
sample.append([('A', '4'), ('C', '5')])
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:43:56+00:00Added an answer on May 26, 2026 at 12:43 pm

    This is a somewhat famous question which was well answered by a famous Pythonista long ago: http://code.activestate.com/recipes/52560-remove-duplicates-from-a-sequence/

    If you can assume equal records are adjacent, there is a recipe in the itertools docs:

    from operator import itemgetter
    from itertools import groupby, imap
    
    def unique_justseen(iterable, key=None):
        "List unique elements, preserving order. Remember only the element just seen."
        # unique_justseen('AAAABBBCCDAABBB') --> A B C D A B
        # unique_justseen('ABBCcAD', str.lower) --> A B C A D
        return imap(next, imap(itemgetter(1), groupby(iterable, key)))
    

    If you can only assume orderable elements, here a variant using the bisect module. Given n inputs with r unique values, its search step costs O(n log r). If a new unique value is found, it is inserted in the seen list for a cost of O(r * r).

    from bisect import bisect_left, insort
    
    def dedup(seq):
        'Remove duplicates. Preserve order first seen.  Assume orderable, but not hashable elements'
        result = []
        seen = []
        for x in seq:
            i = bisect_left(seen, x)
            if i == len(seen) or seen[i] != x:
                seen.insert(i, x)
                result.append(x)
        return result
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use a datastructure like this: building string (indexed) date TDate (indexed)
I have been stumped on this one for a while. I want to take
I have a data structure that represents C# code like this: class Namespace: string
I have a sample xml file that looks like this: <Books> <Category Genre=Fiction BookName=book_name
Is there a datastructure like a Dictionary that allows adding unique elements based on
i have a data structure like this public class Employee { public string Name
My data structure looks like this <datastructure> <field1>data</field1> <field2>data</field2> <field3>data</field3> <field4>data</field4> <field4>data</field4> <field4>data</field4> <field4>data</field4>
I have a data structure which is a collection of tuples like this: things
I have a data structure like this: struct foo { int id; int route;
I have a data structure designed like this: employee . department . building .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.