anekix - 11 months ago 64

Python Question

how to perform union of elements of dict objects of all sublists of a list in python:

ex:

`[`

[

{'a':'b'}

],

[

{ 'c':'d'}

]

]

element at pos 0 in sublist 1 should union with element at pos 0 in sublist 2 Ex OUTPUT:

`[`

[

{

'a':'b',

'c':'d'

}

]

]

Answer Source

What's the expected output of following case?

```
lst = [
[
{'a':'b'}
],
[
{'a':'c'}
]
]
```

[EDIT] Try this.

```
lst = [
[
{'a':'b'}
],
[
{'c':'d'}
]
]
n, size = len(lst), len(lst[0])
for i in xrange(1, n):
for j in xrange(size):
lst[0][j].update(lst[i][j])
print [lst[0]]
```