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 8972637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:15:48+00:00 2026-06-15T18:15:48+00:00

I have a list of tuples. [ (1, 2), (2, 3), (4, 3), (5,

  • 0

I have a list of tuples. [ (1, 2), (2, 3), (4, 3), (5, 6), (6, 7), (8, 2) ]

I want to group them into lists based on which tuples are connected (have related values).

So the end result is two lists of related tuple values =
[ [1, 2, 3, 4, 8], [5, 6, 7] ]

How can I write a function to do this? This was a job interview question. I was trying to do it in Python, but I’m frustrated and just want to see the logic behind the answer, so even psuedo code would help me, so I can see what I did wrong.

I only had a few minutes to do this on the spot, but here is what I tried:

def find_partitions(connections):
 theBigList = []     # List of Lists
 list1 = []          # The initial list to store lists
 theBigList.append(list1)

 for list in theBigList:
 list.append(connection[1[0], 1[1]])
     for i in connections:
         if i[0] in list or i[1] in list:
             list.append(i[0], i[1])

         else:
             newList = []
             theBigList.append(newList)

Essentially, the guy wanted an list of lists of values that were related.
I tried to use a for loop, but realized that it wouldn’t work, and then time ran out.

  • 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-06-15T18:15:49+00:00Added an answer on June 15, 2026 at 6:15 pm

    As we fill in the components, at each stage there are three cases to consider (as you will have to match up overlapping groups):

    1. Neither x or y are in any component already found.
    2. Both are already in different sets, x in set_i and y in set_j.
    3. Either one or both are in one component, x in set_i or y in a set_i.

    We can use the built-in set to help. (see @jwpat’s and @DSM’s trickier examples):

    def connected_components(lst):
        components = [] # list of sets
        for (x,y) in lst:
            i = j = set_i = set_j = None
            for k, c in enumerate(components):
                if x in c:
                    i, set_i = k, c
                if y in c:
                    j, set_j = k, c
    
            #case1 (or already in same set)
            if i == j:
                 if i == None:
                     components.append(set([x,y]))
                 continue
    
            #case2
            if i != None and j != None:
                components = [components[k] for k in range(len(components)) if k!=i and k!=j]
                components.append(set_i | set_j)
                continue
    
            #case3
            if j != None:
                components[j].add(x)
            if i != None:
                components[i].add(y)
    
        return components               
    
    lst = [(1, 2), (2, 3), (4, 3), (5, 6), (6, 7), (8, 2)]
    connected_components(lst)
    # [set([8, 1, 2, 3, 4]), set([5, 6, 7])]
    map(list, connected_components(lst))
    # [[8, 1, 2, 3, 4], [5, 6, 7]]
    
    connected_components([(1, 2), (4, 3), (2, 3), (5, 6), (6, 7), (8, 2)])
    # [set([8, 1, 2, 3, 4]), set([5, 6, 7])] # @jwpat's example
    
    connected_components([[1, 3], [2, 4], [3, 4]]
    # [set([1, 2, 3, 4])] # @DSM's example
    

    This certainly won’t be the most efficient method, but is perhaps one similar to what they would expect. As Jon Clements points out there is a library for these type of calculations: networkx, where they will be much more efficent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of tuples with three values in tuples I want to
I have a list of tuples and want to turn this list into a
I have a list of tuples and I want a new list consisting of
I have a list of tuples which represent the coordinates of a shape -
I have a list of lists of tuples A= [ [(1,2,3),(4,5,6)], [(7,8,9),(8,7,6),(5,4,3)],[(2,1,0),(1,3,5)] ] The
I have a list of lists containing tuples: [[(1L,)], [(2L,)], [(3L,)], [(4L,)], [(5L,)] how
I have a list of tuples of the form (a,b,c,d) and I want to
So I have a list of tuples such as this: [(1,juca),(22,james),(53,xuxa),(44,delicia)] I want this
In python, when you have a list of tuples, you can iterate over them.
I have a rather large list of tuples which contains: [('and', 44023), ('cx', 37711),

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.