cat
cat

Reputation: 263

Is a list in dictionary values?

a = {0:[[1,2,3], [1,3,4,5]]}
print([1,2,3] in a.values())

I get False. Because this list is in values I need True. Is it possible to check all lists in nested list as a value in dictionary? Maybe without loops?

Upvotes: 1

Views: 59

Answers (1)

Nurjan
Nurjan

Reputation: 6063

Since you're using python3 you can do this:

[1,2,3] in list(a.values())[0]

a.values() returns a dictionary view. dictionary views

Then you can wrap the dictionary view into list but this list will contain only one element which can be accessed by index 0. However, if your dictionary contains several keys and corresponding values then list(a.values()) will contain the same number of elements (values mapped to keys) as keys in the dictionary.

Note that when you use some_value in some_collection construct and don't use loops explicitly it will still iterate through the collection.

Upvotes: 1

Related Questions