Is there a way to check if a value is unique for a specific list, within a dictionary of lists? If yes, I would like to know in which key/list pair/s the value is present.
For example: {1: [3, 4, 5], 2: [4], 3: [5], 6: [3, 5, 9], 8: [3 ,8]}
I want to check if 3 is present in any of the other lists within the dictionary, besides 1: [3, 4, 5(compare it with all others). Since it is, then I must receive as a result 6 and 8, because these are the keys, which corresponding lists have this value.
Advertisement
Answer
Try this:
JavaScript
x
16
16
1
def find_n(nums, target, exception):
2
res = []
3
for n, nums_list in nums.items():
4
exception_key = [i for i in exception][0]
5
if n != exception_key and nums_list != exception[exception_key]:
6
if target in nums_list:
7
res.append(n)
8
return res
9
10
11
if __name__ == '__main__':
12
nums = {1: [3, 4, 5], 2: [4], 3: [5], 6: [3, 5, 9], 8: [3, 8]}
13
target = 3
14
exception = {1: [3, 4, 5]}
15
print(find_n(nums, target, exception))
16
Reuslt:
JavaScript
1
2
1
[6, 8]
2