alkopop79
alkopop79

Reputation: 539

Python - compare list elements with dictionary elements

I have a dictionary and a list. I need to compare the "id" tag of the dictionaries (integer) with the list elements. Something like this:

l = [[1], [7], [9]]

dict = {
"first":{
    "id": "0"
},
"second":{
    "id": "7"
},
"third":{
    "id": "4"
}
}

for i in l:
    for j in dict:
        if i == dict[j]["id"]:
            print("Yay!")
        else:
            print("Nay!")

I want to be able to check if any elements of list 'l' can be found in the "id" tags of dictionaries. How do I do that?

Upvotes: 0

Views: 4020

Answers (6)

Tobi G
Tobi G

Reputation: 28

You can do it like this:

l = [[1], [7], [9]]

dict = {
         "first":{"id": "0"},
         "second":{"id": "7"},
         "third":{"id": "4"}
        }

for i in l:
    for j in dict:
        if i[0] == int(dict[j]["id"]):
            print("Yay!")
        else:
            print("Nay!")

Upvotes: 1

Vasilis G.
Vasilis G.

Reputation: 7859

You can also try this one-liner:

l = [[1], [7], [9]]

d = {"first":{ "id": "0"},
     "second":{"id": "7"},
    "third":{"id": "4"}}

l = [elem for elem in l if elem[0] in list(int(value['id']) for value in d.values())]

print(l)

Output:

[[7]]

or alternatively you can do it using filter:

l = [[1], [7], [9]]

d = {"first":{ "id": "0"},
     "second":{"id": "7"},
    "third":{"id": "4"}}

resultValues = list(int(v['id']) for v in d.values())
l = list(filter(lambda i: i[0] in resultValues, l))

print(l)

Output:

[[7]]

Upvotes: 1

Ajax1234
Ajax1234

Reputation: 71471

You can try this:

l = [[1], [7], [9]]

dict = {
"first":{
   "id": "0"
 },
"second":{
 "id": "7"
 },
 "third":{
    "id": "4"
 }
}
final_dicts = [{a:b} for a, b in dict.items() if [int(b['id'])] in l]
print "Yay!" if final_dicts else "Nay"

Output:

"Yay!"

Upvotes: 0

Vivek Harikrishnan
Vivek Harikrishnan

Reputation: 876

As suggested by others please have ids as list instead list of lists. And how about the solution with filter

lst_ids = [1, 7, 9]

my_dict = {
"first":{
    "id": "0"
},
"second":{
    "id": "7"
},
"third":{
    "id": "4"
}
}

filter(lambda k: int(my_dict[k]['id']) in lst_ids, my_dict)

This will return ['second'] that is matched keys of dict.

Upvotes: 1

Ma0
Ma0

Reputation: 15204

I would do it like so:

l = [[1], [7], [9]]

dict_ = {
         "first":{"id": "0"},
         "second":{"id": "7"},
         "third":{"id": "4"}
        }

ids = set(int(v['id']) for _, v in dict_.items())  # set of all ids for quick membership testing

l = [sublist for sublist in l if sublist[0] in ids]  # *
print(l)  # -> [[7]]

I am assuming that you want to modify (re-create) the l list with the items that meet your criteria.


Notes:

  1. Do not use dict as a variable name. You are overwriting the Python built-in.
  2. There is no point to have a list of lists with the sublists being 1-element long. Flatten it (l = [1, 7, 9])

* alternatively, and if all elements in l are single-element lists, you can use the following which will most likely be significantly faster:

l = list(map(lambda x: [x], ids.intersection(x for y in l for x in y)))

Upvotes: 3

Abhijeetk431
Abhijeetk431

Reputation: 846

Print Yay! and Nay! when -- convert to int and check in sub-list?

l = [[1], [7], [9]]

dict1 = {
"first":{
    "id": "0"
},
"second":{
    "id": "7"
},
"third":{
    "id": "4"
}
}

for i in l:
    for j in dict1:
        if int(dict1[j]['id']) in i:
            print("Yay!")
        else:
            print("Nay!")

Upvotes: -1

Related Questions