Search code examples
pythonlistloopstuplesdefaultdict

Trying to call the first element of a tuple by the value of the second element (within a loop)


The title is a bit confusing but I am essentially trying to store the name x of a tuple (x,y) by calling the values of y. The tuples are in a list, and there are three of them. However, this data may change so I want this loop to be able to process any sort of list length as long as the form comes in tuples. Additionally my tuples are in (str, [list]) form. Here is an example of my code.

key, val = list(d.keys()), list(d.values())
for i in val:
    if i == group_ids[0]:
        for x,y in list(d.items()):
            if y == i:
                print(x,y)

I have created a defaultdict previously in order to access my list of tuples in (str, [list]) format because ipywidgets8 no longer accepts dictionaries. After having successfully selected the list of values based on the str in the widgets interface I am looking to store the name of the selected list so that I can save each file based on their respective name. I know with pandas dataframe you can call the value of one column based on the value of another with .loc and I am trying to do something similar with my list of tuples but cannot figure out how. The group_ids variable is the list of selected values received from the ipywidgets button. I have to call [0] position because it has been stored with double brackets. Even more simply put, I want (this is not in any coding language just how my brain can best present this in human words):

FOR i IN val,
if i == group_ids[0]
PRINT x of key at the index where i in val is found  

I hope this explanation is clear. I feel that this should not be so difficult to figure out but for some reason I cannot.

EDIT:

A sample of my data

group_ids = [[5876233, 5883627, 5891029, 5892881, 5896571, 5900242, 5902043, 5905766, 5905796, 5913064, 5913075, 5913080, 5914875, 5920356, 5924048, 5925824, 5927655, 5929456, 5929479, 5931307, 5934950, 5936704, 5940344, 5943972, 5944002, 5945785, 5947627, 5951172, 5951181, 5954751, 5958339, 5958354, 5958358, 5965416, 5965424, 5967145, 5968843, 5972099, 5978640, 5981887, 5983501, 5993193, 5967178, 5967171, 5963649, 5951209, 5929476, 5958331, 5938533, 5933134, 5918577, 5958359]]

list(d.items()) = [('Libraries', [5876233, 5883627, 5891029, 5892881, 5896571, 5900242, 5902043, 5905766, 5905796, 5913064, 5913075, 5913080, 5914875, 5920356, 5924048, 5925824, 5927655, 5929456, 5929479, 5931307, 5934950, 5936704, 5940344, 5943972, 5944002, 5945785, 5947627, 5951172, 5951181, 5954751, 5958339, 5958354, 5958358, 5965416, 5965424, 5967145, 5968843, 5972099, 5978640, 5981887, 5983501, 5993193, 5967178, 5967171, 5963649, 5951209, 5929476, 5958331, 5938533, 5933134, 5918577, 5958359]), ('Sport Facilities', [5812360, 5817970, 5818061, 5821851, 5823750, 5823751, 5827499, 5829344, 5829423, 5831312, 5833208, 5838752, 5840647, 5842526, 5842539, 5842575, 5842583, 5844396....)]

d.keys = dict_keys(['Libraries', 'Sports Facilities', 'Youth Facilities'])

I want the respective key for the list of ids seen in the group_ids variable


Solution

  • So I think you just need:

    for k, v in d.items():
        if v[0] == group_ids[0]:
            print(k, v)
    

    Checking if v == group_ids would be more precise, but I assume that all ids are different.