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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:42:37+00:00 2026-05-10T23:42:37+00:00

I have the following data structure (a list of lists) [ [‘4′, ’21’, ‘1’,

  • 0

I have the following data structure (a list of lists)

[  ['4', '21', '1', '14', '2008-10-24 15:42:58'],   ['3', '22', '4', '2somename', '2008-10-24 15:22:03'],   ['5', '21', '3', '19', '2008-10-24 15:45:45'],   ['6', '21', '1', '1somename', '2008-10-24 15:45:49'],   ['7', '22', '3', '2somename', '2008-10-24 15:45:51'] ] 

I would like to be able to

  1. Use a function to reorder the list so that I can group by each item in the list. For example I’d like to be able to group by the second column (so that all the 21’s are together)

  2. Use a function to only display certain values from each inner list. For example i’d like to reduce this list to only contain the 4th field value of ‘2somename’

so the list would look like this

[      ['3', '22', '4', '2somename', '2008-10-24 15:22:03'],       ['7', '22', '3', '2somename', '2008-10-24 15:45:51'] ] 
  • 1 1 Answer
  • 2 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. 2026-05-10T23:42:38+00:00Added an answer on May 10, 2026 at 11:42 pm

    For the first question, the first thing you should do is sort the list by the second field using itemgetter from the operator module:

    x = [  ['4', '21', '1', '14', '2008-10-24 15:42:58'],   ['3', '22', '4', '2somename', '2008-10-24 15:22:03'],   ['5', '21', '3', '19', '2008-10-24 15:45:45'],   ['6', '21', '1', '1somename', '2008-10-24 15:45:49'],   ['7', '22', '3', '2somename', '2008-10-24 15:45:51'] ]  from operator import itemgetter  x.sort(key=itemgetter(1)) 

    Then you can use itertools’ groupby function:

    from itertools import groupby y = groupby(x, itemgetter(1)) 

    Now y is an iterator containing tuples of (element, item iterator). It’s more confusing to explain these tuples than it is to show code:

    for elt, items in groupby(x, itemgetter(1)):     print(elt, items)     for i in items:         print(i) 

    Which prints:

    21 <itertools._grouper object at 0x511a0> ['4', '21', '1', '14', '2008-10-24 15:42:58'] ['5', '21', '3', '19', '2008-10-24 15:45:45'] ['6', '21', '1', '1somename', '2008-10-24 15:45:49'] 22 <itertools._grouper object at 0x51170> ['3', '22', '4', '2somename', '2008-10-24 15:22:03'] ['7', '22', '3', '2somename', '2008-10-24 15:45:51'] 

    For the second part, you should use list comprehensions as mentioned already here:

    from pprint import pprint as pp pp([y for y in x if y[3] == '2somename']) 

    Which prints:

    [['3', '22', '4', '2somename', '2008-10-24 15:22:03'],  ['7', '22', '3', '2somename', '2008-10-24 15:45:51']] 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following data structure (simplified): A giant list of the class TestClass public
I have something like the following data structure: Category StartDateTime EndDateTime =============================================== 1 12/1/2009
I have following data ct<-structure(list(Conc = c(50L, 100L, 150L, 50L, 100L, 150L, 50L, 100L,
I am creating list for each data items, I have following data structure and
I have the following data: structure(list(Time = c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
I have the following data frame: structure(list(X1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
I have the following data set: structure(list(Geschaeft = c(0.0961028525512254, 0.0753516756309475, 0, 0.0722803347280335, 0, 0.000877706260971328),
I have the following simple data structure. A list of students, each with a
I have the following data frame: structure(list(Species = 1:4, Ni = c(1, NA, 1,
I have the following (dummy) data: d <- structure(list(group = structure(c(1L, 1L, 1L, 1L,

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.