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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:34:00+00:00 2026-05-14T02:34:00+00:00

I have the following list in Python: [[1, 2], [3, 4], [4, 6], [2,

  • 0

I have the following list in Python:

[[1, 2], [3, 4], [4, 6], [2, 7], [3, 9]]

I want to group them into [[1,2,7],[3,4,6,9]]

My code to do this looks like this:

l=[[1, 2], [3, 4], [4, 6], [2, 7], [3, 9]]
lf=[]
for li in l:
    for lfi in lf:
        if lfi.intersection(set(li)):
            lfi=lfi.union(set(li))
            break
    else:
        lf.append(set(li))

lf is my final list. I do a loop over l and lf and when I find an intersection between an element from l and another from lf, I would like to merge them (union)

But I can’t figure out why this is not working. The first to elements of the list l are being inserted with the append command, but the union is not working.
My final list lf looks like [set([1, 2]), set([3, 4])]

It seems to be something pretty basic, but I’m not familiar with sets.
I appreciate any help

Thanks

  • 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-14T02:34:00+00:00Added an answer on May 14, 2026 at 2:34 am

    The problem is here:

    lfi=lfi.union(set(li))
    

    You are not modifying the set. You are creating a new set which is then discarded. The original set is still in the lf array. Use update instead:

    lfi.update(li)
    

    This modifies the original set instead of creating a new one. The result after making this change:

    [set([1, 2, 7]), set([9, 3, 4, 6])]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: SPList list = web.Lists[this.ListName]; SPListItem item = list.Items.Add(); now
i have the following lists in python which i want to recombine into a
I have list in python which has following entries name-1 name-2 name-3 name-4 name-1
I have the following code: List<T> list = new List<T>(); IEnumerable<T> query1 = ...
I have the following code: List<HtmlMeta> metas = new List<HtmlMeta>(); foreach (Control c in
I have the following code: public List<IWFResourceInstance> FindStepsByType(IWFResource res) { List<IWFResourceInstance> retval = new
I have a list of values and I want to put them in a
I'm using Python 2.7. Let's say I have a list like so: string_list =
I have a list in python. This list has sublist in it. for example:
I have a list of booleans in python. I want to AND (or OR

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.