Duplicates / common elements between two lists
<p>I have a stupid question for people who are familiar with lists in Python.
I want to get the common items in two lists. Assuming that I have this list :</p>

<pre><code>dates_list = ['2016-07-08 02:00:02', 
              '2016-07-08 02:00:17', 
              '2016-07-08 02:00:03', 
              '2016-07-08 02:00:20', 
              '2016-07-08 02:01:08', 
              '2016-07-08 02:00:09', 
              '2016-07-08 02:01:22', 
              '2016-07-08 02:01:33']
</code></pre>

<p>And a list named 'time_by_seconds' which contains a lists of all seconds of a day:</p>

<pre><code>time_by_seconds = [['2016-07-08 02:00:00',
          '2016-07-08 02:00:01',
          '2016-07-08 02:00:02',
          '2016-07-08 02:00:03',
          '2016-07-08 02:00:04',
          '2016-07-08 02:00:05',
          '2016-07-08 02:00:06',
          etc                  ],
          ['2016-07-08 02:01:00',
           '2016-07-08 02:01:01',
           '2016-07-08 02:01:02',
           '2016-07-08 02:01:03',
           '2016-07-08 02:01:04',
          etc                  ]]
</code></pre>

<p>This is my code to print the items if they are in this list:</p>

<pre><code>for item in dates_list:
    for one_list in time_by_seconds:
        if item in one_list:
            print item
</code></pre>

<p>This is the result :</p>

<pre><code>2016-07-08 02:00:02
2016-07-08 02:00:17
2016-07-08 02:00:03
2016-07-08 02:00:20
2016-07-08 02:01:08
2016-07-08 02:00:09
2016-07-08 02:01:22
2016-07-08 02:01:33
</code></pre>

<p>But if I use another list, with 49 as length, I have duplicates. Concretely I must have 49 elements as result because all those dates exists in my time_by_seconds. 
This is the list :</p>

<pre><code>beginning_time_list = ['2016-07-08 02:17:42',
 '2016-07-08 02:05:35',
 '2016-07-08 02:03:22',
 '2016-07-08 02:26:33',
 '2016-07-08 02:14:54',
 '2016-07-08 02:05:13',
 '2016-07-08 02:15:30',
 '2016-07-08 02:01:53',
 '2016-07-08 02:02:31',
 '2016-07-08 02:00:08',
 '2016-07-08 02:04:16',
 '2016-07-08 02:08:44',
 '2016-07-08 02:11:17',
 '2016-07-08 02:01:40',
 '2016-07-08 02:04:23',
 '2016-07-08 02:01:34',
 '2016-07-08 02:24:31',
 '2016-07-08 02:00:27',
 '2016-07-08 02:14:35',
 '2016-07-08 02:00:57',
 '2016-07-08 02:02:24',
 '2016-07-08 02:02:46',
 '2016-07-08 02:05:04',
 '2016-07-08 02:11:26',
 '2016-07-08 02:06:24',
 '2016-07-08 02:04:32',
 '2016-07-08 02:08:50',
 '2016-07-08 02:08:27',
 '2016-07-08 02:02:30',
 '2016-07-08 02:03:59',
 '2016-07-08 02:01:19',
 '2016-07-08 02:02:09',
 '2016-07-08 02:05:47',
 '2016-07-08 02:02:36',
 '2016-07-08 02:01:02',
 '2016-07-08 02:02:58',
 '2016-07-08 02:06:19',
 '2016-07-08 02:02:34',
 '2016-07-08 02:00:17',
 '2016-07-08 02:10:03',
 '2016-07-08 02:08:20',
 '2016-07-08 02:02:36',
 '2016-07-08 02:17:25',
 '2016-07-08 02:07:19',
 '2016-07-08 02:13:07',
 '2016-07-08 02:03:51',
 '2016-07-08 02:03:35',
 '2016-07-08 02:14:53',
 '2016-07-08 02:18:36']
</code></pre>

<p>The same code :</p>

<pre><code>for item in beginning_time_list:
    for one_list in time_by_seconds:
        if item in one_list:
            print item
</code></pre>

<p>And this is the result :</p>

<pre><code>2016-07-08 02:17:42
2016-07-08 02:17:42
2016-07-08 02:17:42
2016-07-08 02:17:42
2016-07-08 02:05:35
2016-07-08 02:05:35
2016-07-08 02:03:22
2016-07-08 02:26:33
2016-07-08 02:26:33
2016-07-08 02:26:33
2016-07-08 02:26:33
2016-07-08 02:26:33
2016-07-08 02:26:33
2016-07-08 02:14:54
2016-07-08 02:14:54
2016-07-08 02:14:54
2016-07-08 02:05:13
2016-07-08 02:05:13
2016-07-08 02:15:30
2016-07-08 02:15:30
2016-07-08 02:15:30
2016-07-08 02:15:30
2016-07-08 02:01:53
2016-07-08 02:02:31
2016-07-08 02:00:08
2016-07-08 02:04:16
2016-07-08 02:08:44
2016-07-08 02:08:44
2016-07-08 02:11:17
2016-07-08 02:11:17
2016-07-08 02:11:17
2016-07-08 02:01:40
2016-07-08 02:04:23
2016-07-08 02:01:34
2016-07-08 02:24:31
2016-07-08 02:24:31
2016-07-08 02:24:31
2016-07-08 02:24:31
2016-07-08 02:24:31
2016-07-08 02:00:27
2016-07-08 02:14:35
2016-07-08 02:14:35
2016-07-08 02:14:35
2016-07-08 02:00:57
2016-07-08 02:02:24
2016-07-08 02:02:46
2016-07-08 02:05:04
2016-07-08 02:05:04
2016-07-08 02:11:26
2016-07-08 02:11:26
2016-07-08 02:11:26
2016-07-08 02:06:24
2016-07-08 02:06:24
etc
</code></pre>

<p>Sorry there are 95 items !</p>

<p>Someone knows why I have duplicates?
Thnx</p>

<pre><code>import collections

def flatten(iterable):
    for item in iterable:
        if isinstance(item, (str, bytes)):
            yield item
        if isinstance(item, collections.Sequence):
            yield from flatten(item)
        else:
            yield item


a = [1, 6, 10]
b = [[0, 1, 2], 3, [4], [5, (6, 7), 8], 9]

common_items = set(a) &amp; set(flatten(b))
</code></pre>

python list python-2.7 for-loop