Split and flatten a list of strings

  • A+
Category:Languages
lst = [   "Zambia",   "Zimbabwe",   "Suite,203,2880,Zanker,Rd,San,Jose,95134",   "1496A,1st,and,2nd,Floor,19th,main,8th,crossSector,1,HSR,Layout,Bengaluru,560102", ] 

here i have a lst of world . some are actual words just example("Zambia") and some are like sentence just example ("Suite,203,2880,Zanker,Rd,San,Jose,95134")

How can i convert them into below format.

lst = [   "Zambia",   "Zimbabwe",   "Suite",   "203",   "2880",   "Zanker",   "Rd",   "San",   "Jose",   "95134,   "1496A",   "1st",   "and",   "2nd",   "Floor",   "19th",   "main",   "8th",   "crossSector",   "1",   "HSR",   "Layout",   "Bengaluru",   "560102",   "g2crowd_badge2",   "Created with Sketch." ] 

How can i convet my list into this format using python

PLease have a look into this.

 


try:

res = []     for i in lst:    res.extend(i.split(","))    

Another option is to use reduce:

res = list(reduce(lambda a, b: a + b.split(','), lst, [])) 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: