Pop out the whole dic if element of 1st dic in list is repeated?
<p>Here I have a list of dic, where I want to remove duplicates which is equal to 1st element of dic.
Input :</p>

<pre><code>data  = [

    [
          [{'color': '1'},{'color': '0'},{'color': '2'},{'color': '1'}],
          [{'color': '2'},{'color': '3'},{'color': '2'},{'color': '5'}],
          [{'color': '1'},{'color': '1'},{'color': '3'},{'color': '3'}]
    ],

    [
          [{'color': '1'},{'color': '1'},{'color': '4'},{'color': '4'}],
          [{'color': '4'},{'color': '3'},{'color': '1'},{'color': '4'}],
          [{'color': '7'},{'color': '1'},{'color': '7'},{'color': '1'}]
    ]

        ]
</code></pre>

<p>I have tried and got the expected output, Is there any pythonic way to achieve the same ?</p>

<p>Code :</p>

<pre><code>new = [] ; 
for i in data:
    master = []
    for j in i:
        temp = []
        for k in j:
            if j[0]['color'] != k['color']:
                temp.append(k)
        temp.insert(0,j[0])
        master.append(temp)
    new.append(master)
print(new)
</code></pre>

<p>Expected Output :</p>

<pre><code>data  = [

    [
          [{'color': '1'},{'color': '0'},{'color': '2'}],
          [{'color': '2'},{'color': '3'},{'color': '5'}],
          [{'color': '1'},{'color': '3'},{'color': '3'}]
    ],

    [
          [{'color': '1'},{'color': '4'},{'color': '4'}],
          [{'color': '4'},{'color': '3'},{'color': '5'}],
          [{'color': '7'},{'color': '1'},{'color': '1'}]
    ]     
    ]
</code></pre>

<p>No need for all the temporary lists:</p>

<pre><code>for item in data:
    for i,subitem in enumerate(item):
        item[i] = [item[i][0]] + [dct for dct in item[i][1:]
                                  if dct['color'] != item[i][0]['color']]
</code></pre>

<p>Basically it just iterates through each of the subitems and replaces it with its first item plus the rest of the items that don't have the same value.</p>

<p>Although it's ugly, you can even reduce it to a one-liner (for illustrative purposes only... I wouldn't recommend it because readability):</p>

<pre><code>data = [[[item[i][0]] + [dct for dct in item[i][1:] if dct['color'] != item[i][0]['color']] for i,subitem in enumerate(item)] for item in data]
</code></pre>

python list dictionary