-1

How can I do the following in Python?

a = [2,[33,4],[2,3,4,6]]
li = [ i for i in a if isinstance(i,int) else j in i ]

how do i convert list a into a = [2,33,4,2,3,4,6]

I am able to do it with normal for loop but i want to use only list comprehension

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
hsekol
  • 153
  • 1
  • 2
  • 11
  • 2
    Don't. A list comprehension isn't the right tool for this job. – Aran-Fey May 22 '18 at 07:33
  • see https://stackoverflow.com/questions/2158395/flatten-an-irregular-list-of-lists?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa as well – Mazdak May 22 '18 at 07:36

1 Answers1

0

You can use:

In [20]: [k for e in a for k in (e if isinstance(e, list) else [e])]
    ...: 
Out[20]: [2, 33, 4, 2, 3, 4, 6]
llllllllll
  • 16,169
  • 4
  • 31
  • 54