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

  • SEARCH
  • Home
  • 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 858403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:32:23+00:00 2026-05-15T08:32:23+00:00

I have two nested lists of different sizes: A = [[1, 7, 3, 5],

  • 0

I have two nested lists of different sizes:

A = [[1, 7, 3, 5], [5, 5, 14, 10]]

B = [[1, 17, 3, 5], [1487, 34, 14, 74], [1487, 34, 3, 87], [141, 25, 14, 10]]

I’d like to gather all nested lists from list B if A[2:4] == B[2:4] and put it into list L:

L = [[1, 17, 3, 5], [141, 25, 14, 10]]

Additionally if the match occurs then I want to change last element of sublist B into first element of sublist A so the final solution would look like this:

L1 = [[1, 17, 3, 1], [141, 25, 14, 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-15T08:32:24+00:00Added an answer on May 15, 2026 at 8:32 am

    It looks like this does what you want:

    > [b for b in B if b[2:4] in [a[2:4] for a in A]]
    [[1, 17, 3, 5], [141, 25, 14, 10]]
    

    But, for efficiency’s sake, you may want to precompute the slices of A.

    > a_slices = [a[2:4] for a in A]
    > [b for b in B if b[2:4] in a_slices]
    [[1, 17, 3, 5], [141, 25, 14, 10]]
    

    Here’s something that looks like it meets your new requirements:

    > [b[:-1] + a[:1] for b in B for a in A if b[2:4] == a[2:4]]
    [[1, 17, 3, 1], [141, 25, 14, 5]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three lists. Two are returned from functions. The other list, list_c ,
I have two tables: Campaigns, Campaign_statistics. I need to output campaigns list with nested
I have two nested packages that I am calling via fast_cgi. From the first
I have two nested loop in XSL like this, at this moment I use
So I have some nested lists (only one level deep) and I'm running into
I have two sortable lists, one being nested, with a mouse over effect on
I have a fairly large, three level deep, menu consisting of nested lists like
I have two entity lists being retrieved from the database they are using the
I have a LINQ query which is fetching a list of nested objects. from
I have two masterpages. A main.Master and a search.Master. The search.Master is a nested

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.