Getting key values from a dictionary
<p>I have a dictionary with indices as keys and timestamps as values. I wanted to get the keys for whose values there is a overlap.</p>

<p>ex:</p>

<pre><code>{1: 19-13-30
    19-13-32
    19-13-33
    .
    .
    19-13-55,
 2: 19-13-25
    19-13-26
    19-13-27
    .
    .
    19-13-35,
  3:19-13-10
    19-13-31
    .
    .
    19-13-18}
</code></pre>

<p>For the above dictionary values of 1 and 2 overlap(19-13-30 to 19-13-35). So, I want to return the keys whenever there is a overlap. In this case 1 &amp; 2</p>

<p>For calculating the overlap I am iterating over the values and storing the start time and end time in a list like [starttime,endtime]. Then I am checking for overlap by </p>

<pre><code>   overlapping = [ [x,y] for x in intervals for y in intervals if x is not y and x[1]&gt;y[0] and x[0]&lt;y[0] ]
for x in overlapping:
    print '{0} overlaps with {1}'.format(x[0],x[1])
</code></pre>

<p>This prints the values which overlap.But instead I want the keys whose values overlap.</p>

<p>Instead of iterating over the intervals, you can iterate over the values (i.e. the positions in your interval list). Something like</p>

<pre><code>keys = range(len(intervals))
overlapping = [ [x,y] for x in keys for y in keys if x is not y and intervals[x][1]&gt;intervals[y][0] and intervals[x][0]&lt;intervals[y][0] ]
</code></pre>

python dictionary