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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:30:34+00:00 2026-06-18T03:30:34+00:00

given a list with sublists, I want to extract the sublists with specified length.

  • 0

given a list with sublists, I want to extract the sublists with specified length.
If the sublist has length less than the specified, then extract all. Kindly see below for clarification

In the below example, I am extracting for sublists with length = 2. If length is greater, I extract the first two elements in sublist and ignore the remaining.
Input

A = [['A',[1,2,3]],['D',[3,4]],['E',[6,7]],['F',[1]],['G',[7,6,5,4]]]

Output

B = [['A',[1,2]],['D',[3,4]],['E',[6,7]],['F',[1]],['G',[7,6]]]

I am currently doing as follows, it works, but wondering if there is a simple way

B=[]

for el in A:
  l = []

  if len(el[1]) > 2:
     l.append(el[0])
     l.append(el[1][0:2])
     B.append(l)

  else:
     l.append(el[0])
     l.append(el[1][0:2])
     B.append(l)

print B
  • 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-18T03:30:35+00:00Added an answer on June 18, 2026 at 3:30 am

    you can make use of list comprehension for this:

    In [71]: A = [['A',[1,2,3]],['D',[3,4]],['E',[6,7]],['F',[1]],['G',[7,6,5,4]]]
    
    In [72]: [[x[0],x[1][:2]] for x in A]
    Out[72]: [['A', [1, 2]], ['D', [3, 4]], ['E', [6, 7]], ['F', [1]], ['G', [7, 6]]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make prolog returning me all possible sublists of given list so
I want to generate all the sublists of a given list with the given
Given List {1,2,3,4,5} I want to generate all possible sequences of those numbers in
Given a list a containing vectors of unequal length and a vector b containing
Given a two-dimensional list, I would like to find everything that contains a sublist.
I'm trying to remove of all members of a list appearing after a sublist
I coded a function to enumerate all permutations of a given list. What do
I'm writing this function which I want to print all the sublists of a
99 scala problems has this question: Pack consecutive duplicates of list elements into sublists.
The opposite of list flattening. Given a list and a length n return a

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.