0

I have a list with multiple dicts in a list. I want to combine the list in to one. In here, i am using for loop to merge but unable to do it. Help me with some solutions. the multiple list in the list of dicts in the datas can be increased during execution. so i am using for loop to get it

Here's the sample code:

datas = [
[{'state': 'tamil nadu', 'city': 'tirunelveli', 'haps': 'hap0', 'serial': '1'}, 
{'state': 'tamil nadu', 'city': 'nagerkoil', 'haps': 'hap0', 'serial': '2'}, 
{'state': 'tamil nadu', 'city': 'tuticorin', 'haps': 'hap0', 'serial': '3'}, 
{'state': 'tamil nadu', 'city': 'madurai', 'haps': 'hap0', 'serial': '4'},
{'state': 'tamil nadu', 'city': 'chennai', 'haps': 'hap0', 'serial': '5'}],
[{'state': 'kerala', 'city': 'palakad', 'haps': 'hap1', 'serial': '6'}, 
{'state': 'kerala', 'city': 'guruvayor', 'haps': 'hap1', 'serial': '7'}, 
{'state': 'kerala', 'city': 'kolikodu', 'haps': 'hap1', 'serial': '8'}, 
{'state': 'kerala', 'city': 'kottayam', 'haps': 'hap1', 'serial': '9'}, 
{'state': 'kerala', 'city': 'idukki', 'haps': 'hap1', 'serial': '10'}],
[]
]
d = []
def throttle(d):
    if data !=[]:
        d.append(data)
        print(d)
    else:
        print("no data")   

for data in datas:
    throttle(data)

output i got:

[{'city': 'tirunelveli', 'serial': '1', 'haps': 'hap0', 'state': 'tamil nadu'}, {'city': 'nagerkoil', 'serial': '2', 'haps': 'hap0', 'state': 'tamil nadu'}, {'city': 'tuticorin', 'serial': '3', 'haps': 'hap0', 'state': 'tamil nadu'}, {'city': 'madurai', 'serial': '4', 'haps': 'hap0', 'state': 'tamil nadu'}, {'city': 'chennai', 'serial': '5', 'haps': 'hap0', 'state': 'tamil nadu'}, [...]]
[{'city': 'palakad', 'serial': '6', 'haps': 'hap1', 'state': 'kerala'}, {'city': 'guruvayor', 'serial': '7', 'haps': 'hap1', 'state': 'kerala'}, {'city': 'kolikodu', 'serial': '8', 'haps': 'hap1', 'state': 'kerala'}, {'city': 'kottayam', 'serial': '9', 'haps': 'hap1', 'state': 'kerala'}, {'city': 'idukki', 'serial': '10', 'haps': 'hap1', 'state': 'kerala'}, [...]]
no data

Required output:

[{'city': 'tirunelveli', 'serial': '1', 'haps': 'hap0', 'state': 'tamil nadu'}, {'city': 'nagerkoil', 'serial': '2', 'haps': 'hap0', 'state': 'tamil nadu'}, {'city': 'tuticorin', 'serial': '3', 'haps': 'hap0', 'state': 'tamil nadu'}, {'city': 'madurai', 'serial': '4', 'haps': 'hap0', 'state': 'tamil nadu'}, {'city': 'chennai', 'serial': '5', 'haps': 'hap0', 'state': 'tamil nadu'},{'city': 'palakad', 'serial': '6', 'haps': 'hap1', 'state': 'kerala'}, {'city': 'guruvayor', 'serial': '7', 'haps': 'hap1', 'state': 'kerala'}, {'city': 'kolikodu', 'serial': '8', 'haps': 'hap1', 'state': 'kerala'}, {'city': 'kottayam', 'serial': '9', 'haps': 'hap1', 'state': 'kerala'}, {'city': 'idukki', 'serial': '10', 'haps': 'hap1', 'state': 'kerala'}]
no data
Smack Alpha
  • 1,828
  • 1
  • 17
  • 37
  • 3
    Are you trying to flatten a list? Does this help? https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-list-of-lists – dyz May 07 '19 at 04:55

3 Answers3

4

You can do:

print([x for li in datas for x in li])

Output:

 [{'state': 'tamil nadu', 'city': 'tirunelveli', 'haps': 'hap0', 'serial': '1'}, {'state': 'tamil nadu', 'city': 'nagerkoil', 'haps': 'hap0', 'serial': '2'}, {'state': 'tamil nadu', 'city': 'tuticorin', 'haps': 'hap0', 'serial': '3'}, {'state': 'tamil nadu', 'city': 'madurai', 'haps': 'hap0', 'serial': '4'}, {'state': 'tamil nadu', 'city': 'chennai', 'haps': 'hap0', 'serial': '5'}, {'state': 'kerala', 'city': 'palakad', 'haps': 'hap1', 'serial': '6'}, {'state': 'kerala', 'city': 'guruvayor', 'haps': 'hap1', 'serial': '7'}, {'state': 'kerala', 'city': 'kolikodu', 'haps': 'hap1', 'serial': '8'}, {'state': 'kerala', 'city': 'kottayam', 'haps': 'hap1', 'serial': '9'}, {'state': 'kerala', 'city': 'idukki', 'haps': 'hap1', 'serial': '10'}]
R4444
  • 2,016
  • 2
  • 19
  • 30
3

Looks like a good use case for itertools.chain.from_iterable:

>>> from itertools import chain
>>> list(chain.from_iterable(datas))
[{'state': 'tamil nadu', 'city': 'tirunelveli', 'haps': 'hap0', 'serial': '1'}, {'state': 'tamil nadu', 'city': 'nagerkoil', 'haps': 'hap0', 'serial': '2'}, {'state': 'tamil nadu', 'city': 'tuticorin', 'haps': 'hap0', 'serial': '3'}, {'state': 'tamil nadu', 'city': 'madurai', 'haps': 'hap0', 'serial': '4'}, {'state': 'tamil nadu', 'city': 'chennai', 'haps': 'hap0', 'serial': '5'}, {'state': 'kerala', 'city': 'palakad', 'haps': 'hap1', 'serial': '6'}, {'state': 'kerala', 'city': 'guruvayor', 'haps': 'hap1', 'serial': '7'}, {'state': 'kerala', 'city': 'kolikodu', 'haps': 'hap1', 'serial': '8'}, {'state': 'kerala', 'city': 'kottayam', 'haps': 'hap1', 'serial': '9'}, {'state': 'kerala', 'city': 'idukki', 'haps': 'hap1', 'serial': '10'}]
RoadRunner
  • 25,803
  • 6
  • 42
  • 75
1

the reason you can not get what you want is that you have nested list in datas, so you should extend the list, not append.

you can try this:

d = []
for data in datas:
    if data:
        d += data

or use filter and reduce, filter is used to filter empty list, and reduce to combine.

from functools import reduce
import operator
d = list(reduce(operator.__add__, filter(None, datas)))
recnac
  • 3,744
  • 6
  • 24
  • 46