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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:45:01+00:00 2026-06-14T18:45:01+00:00

Can You help me to convert Python list: [(1, ‘a’), (2, ‘b’), (2, ‘c’),

  • 0

Can You help me to convert Python list:

[(1, 'a'), (2, 'b'), (2, 'c'), (3, 'd'), (3, 'e')]

so that:
(1, 'a') is index 0

(2, 'b'), (2, 'c') are both index 1

(3, 'd'), (3, 'e') are both index 2

Simply, all tuples which element[0] is equal, have same index.

Thank You,

  • 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-14T18:45:02+00:00Added an answer on June 14, 2026 at 6:45 pm

    itertools.groupby to the rescue!:

    lst = [(1, 'a'), (2, 'b'), (2, 'c'), (3, 'd'), (3, 'e')]
    lst.sort(key=lambda x: x[0])  #Only necessary if your list isn't sorted already.
    new_lst = [list(v) for k,v in itertools.groupby(lst,key=lambda x:x[0])]
    

    You could use operator.itemgetter(0) instead of the lambda if you wanted…

    demo:

    >>> import itertools
    >>> lst = [(1, 'a'), (2, 'b'), (2, 'c'), (3, 'd'), (3, 'e')]
    >>> lst.sort(key=lambda x: x[0])
    >>> new_lst = [list(v) for k,v in itertools.groupby(lst,key=lambda x:x[0])]
    >>> new_lst
    [[(1, 'a')], [(2, 'b'), (2, 'c')], [(3, 'd'), (3, 'e')]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can somebody help me convert this VB function to Perl (PHP, Ruby or Python
Is there any compiler which will help me convert my python code to proper
Can someone please help me convert the following two lines of python to C#.
I'm looking for a decorator for Python class that would convert any element access
Can anyone help convert this this actionscript to Objective-c? if(mcMain.y >= stage.stageHeight - mcMain.height)
Can somebody help me to convert the following array to a hash with the
can you guys help to convert a JPG File into an 2-dimensional int []
can someboody please help me convert these strings/ints into a usable font thing? it
Can you help me look for a way to convert char into 8x8 binary
Can any body help me to convert the following SQL query in LinqToSql syntax.

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.