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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:03:06+00:00 2026-05-11T20:03:06+00:00

I have been poking around for a recipe / example to index a list

  • 0

I have been poking around for a recipe / example to index a list of tuples without taking a modification of the decorate, sort, undecorate approach.

For example:

l=[(a,b,c),(x,c,b),(z,c,b),(z,c,d),(a,d,d),(x,d,c) . . .]

The approach I have been using is to build a dictionary using defaultdict of the second element

from collections import defaultdict

tdict=defaultdict(int)

for myTuple in l:
    tdict[myTuple[1]]+=1

Then I have to build a list consisting of only the second item in the tuple for each item in the list. While there are a number of ways to get there a simple approach is to:

tempList=[myTuple[1] for myTuple in l]

and then generate an index of each item in tdict

indexDict=defaultdict(dict)
for key in tdict:
    indexDict[key]['index']=tempList.index(key)

Clearly this does not seem very Pythonic. I have been trying to find examples or insights thinking that I should be able to use something magical to get the index directly. No such luck so far.

Note, I understand that I can take my approach a little more directly and not generating tdict.

output could be a dictionary with the index

indexDict={'b':{'index':0},'c':{'index':1},'d':{'index':4},. . .}

After learning a lot from Nadia’s responses I think the answer is no.

While her response works I think it is more complicated than needed. I would simply

 def build_index(someList):
    indexDict={}
    for item in enumerate(someList):
        if item[1][1] not in indexDict:
           indexDict[item[1][1]]=item[0]
    return indexDict
  • 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-11T20:03:07+00:00Added an answer on May 11, 2026 at 8:03 pm

    This will generate the result you want

    dict((myTuple[1], index) for index, myTuple in enumerate(l))
    
    >>> l = [(1, 2, 3), (4, 5, 6), (1, 4, 6)]
    >>> dict((myTuple[1], index) for index, myTuple in enumerate(l))
    {2: 0, 4: 2, 5: 1}
    

    And if you insist on using a dictionary to represent the index:

    dict((myTuple[1], {'index': index}) for index, myTuple in enumerate(l))
    

    The result will be:

    {2: {'index': 0}, 4: {'index': 2}, 5: {'index': 1}}
    

    EDIT
    If you want to handle key collision then you’ll have to extend the solution like this:

    def build_index(l):
        indexes = [(myTuple[1], index) for index, myTuple in enumerate(l)]
        d = {}
        for e, index in indexes:
            d[e] = min(index, d.get(e, index))
        return d
    
    >>> l = [(1, 2, 3), (4, 5, 6), (1, 4, 6), (2, 4, 6)]
    >>> build_index(l)
    {2: 0, 4: 2, 5: 1}
    

    EDIT 2

    And a more generalized and compact solution (in a similar definition to sorted)

    def index(l, key):
        d = {}
        for index, myTuple in enumerate(l):
            d[key(myTuple)] = min(index, d.get(key(myTuple), index))
        return d
    
    >>> index(l, lambda a: a[1])
    {2: 0, 4: 2, 5: 1}
    

    So the answer to your question is yes: There is a way in Python to index a list of containers (tuples, lists, dictionaries) by an element of a container without preprocessing. But your request of storing the result in a dictionary makes it impossible to be a one liner. But there is no preprocessing here. The list is iterated only once.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been poking around and have been unable to find a standard for
I have been poking around in PHP for OOP and I noticed something... Objects
Have been struggling with Javascript closure for a while trying to wrap brain around
Have been reading around on IErrorHandler and want to go the config route. so,
I've been poking around with R graphical parameters trying to make my plots look
So I've been poking around with C# a bit lately, and all the Generic
Alright, so I've been doing some poking around, and I realize my problem, but
Have been looking at the MVC storefront and see that IQueryable is returned from
Have been studying the file system related classes of Adobe AIR 1.5, but so
We have been using CruiseControl for quite a while with NUnit and NAnt. For

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.