0

This is the list i am trying to flatten.

pp=['a1b0c00',['00ffbcd','c2df']]
flat_list = [item for sublist in pp for item in sublist]
flat_list: ['a', '1', 'b', '0', 'c', '0', '0', '00ffbcd', 'c2df']

Expected result is a single list:['a1b0c00','00ffbcd','c2df'] .

How to flatten this kind of lists in python?

9113303
  • 852
  • 1
  • 16
  • 30
  • @jpp: [This](https://stackoverflow.com/questions/5286541/how-can-i-flatten-lists-without-splitting-strings) is a more relevant dupe related to strings – Sheldore Jan 02 '19 at 11:04
  • 1
    @Bazingaa, Thanks, added to the list. – jpp Jan 02 '19 at 11:05

1 Answers1

4

This is one approach.

pp=['a1b0c00',['00ffbcd','c2df']]
res = []
for i in pp:
    if isinstance(i, list):
        res.extend(i)
    else:
        res.append(i)
print( res )

Output:

['a1b0c00', '00ffbcd', 'c2df']
Rakesh
  • 81,458
  • 17
  • 76
  • 113