<p>I am assuming you only want keys where <em>both</em> tuple items are present. If so, this should work:</p>
<pre class="lang-py prettyprint-override"><code>no_match = []
for item in list1:
    item1, item2 = item
    wanted_keys = [
        key for key in dict1.keys() if
        item1 in dict1[key] and
        item2 in dict1[key]
    ]
    if len(wanted_keys) &gt; 0:
        print(&quot;%s:  %s&quot; % (item, wanted_keys))
    else:
        no_match.append(item)
print(&quot;No matches found for items:&quot;, no_match)
</code></pre>
<p>The output looks like this:</p>
<pre><code>('A', 'B'):  ['chr1:1000-2000', 'chr3:1200-3000']
('A', 'C'):  ['chr1:1000-2000', 'chr3:1200-3000']
('A', 'X'):  ['chr2:999-2000']
('A', 'Y'):  ['chr2:999-2000']
('B', 'C'):  ['chr1:1000-2000', 'chr3:1200-3000']
('X', 'Y'):  ['chr2:999-2000']
No matches found for items: [('B', 'X'), ('B', 'Y'), ('C', 'X'), ('C', 'Y')]
</code></pre>
<p>There is probably a better, more elegant way of doing this, I am not a python person, but this is the best I could come up with.</p>

CC BY-SA 4.0
2021-11-16T18:04:43.897Z
2021-11-16T18:04:43.897Z
terdon
<pre><code>dict1 = {'chr1:1000-2000': ['A', 'B', 'C'], 'chr2:999-2000': ['A', 'X', 'Y'], 'chr3:1200-3000': ['B', 'A', 'C']}

list1 = [('A', 'B'), ('A', 'C'), ('A', 'X'), ('A', 'Y'), ('B', 'C'), ('B', 'X'), ('B', 'Y'), ('C', 'X'), ('C', 'Y'), ('X', 'Y')]
</code></pre>
<p>I want to map the items in list1 eg('A','B') both in dict1 values and return keys in wich this group occur</p>

CC BY-SA 4.0
2021-11-16T17:51:44.693Z
Please [edit] your question and show us the expected output. Should _both_ keys be there or is one enough? Can they be in any order?
terdon
CC BY-SA 4.0
2023-09-20T09:02:07.053Z
edited thank you
amardeep
<python><sequence-analysis>
How to map list items which are in groups in the dictionary values and return keys corrosponding it?
# How to map list items which are in groups in the dictionary values and return keys corrosponding it?

(Asked by: username_0 on 2021-11-16)

dict1 = {'chr1:1000-2000': ['A', 'B', 'C'], 'chr2:999-2000': ['A', 'X', 'Y'], 'chr3:1200-3000': ['B', 'A', 'C']}

list1 = [('A', 'B'), ('A', 'C'), ('A', 'X'), ('A', 'Y'), ('B', 'C'), ('B', 'X'), ('B', 'Y'), ('C', 'X'), ('C', 'Y'), ('X', 'Y')]

I want to map the items in list1 eg('A','B') both in dict1 values and return keys in wich this group occur

**Tags:** python, sequence-analysis
> **Comments:**
> **username_1**: Please [edit] your question and show us the expected output. Should _both_ keys be there or is one enough? Can they be in any order?  
> **username_0**: edited thank you  

---

## 1 answers

### Answer by username_1 on 2021-11-16. Score: 1 | Accepted answer

I am assuming you only want keys where both tuple items are present. If so, this should work:
no_match = []
for item in list1:
    item1, item2 = item
    wanted_keys = [
        key for key in dict1.keys() if
        item1 in dict1[key] and
        item2 in dict1[key]
    ]
    if len(wanted_keys) > 0:
        print("%s:  %s" % (item, wanted_keys))
    else:
        no_match.append(item)
print("No matches found for items:", no_match)

The output looks like this:
('A', 'B'):  ['chr1:1000-2000', 'chr3:1200-3000']
('A', 'C'):  ['chr1:1000-2000', 'chr3:1200-3000']
('A', 'X'):  ['chr2:999-2000']
('A', 'Y'):  ['chr2:999-2000']
('B', 'C'):  ['chr1:1000-2000', 'chr3:1200-3000']
('X', 'Y'):  ['chr2:999-2000']
No matches found for items: [('B', 'X'), ('B', 'Y'), ('C', 'X'), ('C', 'Y')]

There is probably a better, more elegant way of doing this, I am not a python person, but this is the best I could come up with.


---

