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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:20:46+00:00 2026-06-12T20:20:46+00:00

I have some dozens of tuples each containing 2 strings and 1 integer. Ex:

  • 0

I have some dozens of tuples each containing 2 strings and 1 integer. Ex:(str, str, int).
All these tuples are in a list (example below).
Each tuple is unique and each tuple’s strings and integer are also unique.

Ex.:

[('a','aA', 53),
 ('b','bb', 21),
 ('c','cc', 234),
 ('d','de', 76),
..]

What I want is, to use this data structure like a dictionary and retrieve the entire tuple for any of one of the 3 values I pass.

Ex.:

For value 'a' -> get the whole tuple of:('a', 'aA', 53)

For value 'cc' -> get the whole tuple of:('c', 'cc', 234)

For value '76' – > get the whole tuple of:('d', 'de', 76)

So far I have done:
Creating a simple function to iterate through the list of tuples, go through each tuple and its all 3 values to find a match and if there’s a match return the tuple, if not return False.

This sounds slow and seems like the very wrong way to do this task.

  1. What would be the right way to achieve this?
  2. Should I create 3 dictionaries and link them to each other?
  • 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-12T20:20:47+00:00Added an answer on June 12, 2026 at 8:20 pm

    You’d have to create separate indexes using dictionaries to allow looking up elements by contents:

    from collections import defaultdict
    
    index_on_1 = defaultdict(list)
    index_on_2 = defaultdict(list)
    index_on_3 = defaultdict(list)
    
    for i, (val1, val2, val3) in enumerate(yourstructure):
        index_on_1[val1].append(i)
        index_on_2[val2].append(i)
        index_on_3[val3].append(i)
    

    Now you can look up indices on strings:

    from itertools import chain
    
    def lookup(entry):
        if isinstance(entry, str):
            entries = chain(index_on_1.get(entry, []), index_on_2.get(entry, []))
            return [yourstructure[i] for i in entries]
        else:
            return [yourstructure[i] for i in index_on_3.get(entry, [])]
    

    Note that this always returns a list, as your entries could match multiple tuples. If the lookup is a string, we only use the first two indexes, otherwise only the 3rd.

    Alternatively, the more general solution that doesn’t care about entry type, would be to create a list of indexes, instead of 3 separate variables:

    indexes = [defaultdict(list) for _ in range(3)]
    
    for i, values in enumerate(yourstructure):
        for index, val in zip(indexes, values):
            index[val].append(i)
    

    with the lookup becoming:

    def lookup(entry):
        entries = chain(*[index.get(entry, []) for index in indexes])
        return [yourstructure[i] for i in entries]
    

    You could bundle this all up in a class, where your indexes are kept up to date as you add or remove elements.

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

Sidebar

Related Questions

I have dozens of local, child websites under my national website. Some of these
I have dozens of tables in an existing MSSQL database all with autonumber ID
I have some relational MySQL database with dozens of tables... My query is 1000
I have some HTML code that is created dynamically and includes potentially dozens of
Imagine you have a class with dozens of private member variables. Each member variable
I have some code reading in and doing work on dozens of input sources.
We have a system in which each thread (there can be dozens of them)
I have dozens of perl script on linux server, and I want use some
I have dozens of these warnings in my project and I'd like to clean
I have some data loaded as a np.ndarray and need to convert it to

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.