How to convert nested list into a single list in python? I have a list that contain id number. Some elements of the list is an another list. To convert nested list into a single list, I write a recursive function using collections module. 

My codes are following.

from collections import Iterable
def single_list(list):
for item in list:
    if isinstance(item, Iterable):
        yield from single_list(item)
    else:
        yield item

Item_list = [10,20,[30,40],[50,'Null',70],100]
items_single=single_list(Item_list)
for item in items_single:
print(item)


When I run my program, I get the following error message.

  Traceback (most recent call last):
  File "/Research/SoftDev/SEPJ/StackOverflow_qs.py", line 42, in &lt;module&gt;
  for i in items_single:
  File "/Research/SoftDev/SEPJ/StackOverflow_qs.py", line 36, in single_list
  yield from single_list(item)
  File "/Research/SoftDev/SEPJ/StackOverflow_qs.py", line 36, in single_list
  yield from single_list(item)
  File "/Research/SoftDev/SEPJ/StackOverflow_qs.py", line 36, in single_list
  yield from single_list(item)
  [Previous line repeated 986 more times]
  File "/Research/SoftDev/SEPJ/StackOverflow_qs.py", line 35, in single_list
  if isinstance(item, Iterable):
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/abc.py", line 184, in __instancecheck__
if subclass in cls._abc_cache:
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_weakrefset.py", line 75, in __contains__
return wr in self.data
 RecursionError: maximum recursion depth exceeded in comparison

 Process finished with exit code 1


I do not know how to fix the error.
You are getting the error because of string value contains in the nested list.
You need to handle string value as well.

You can fix the error by following codes:

from collections import Iterable

def single_list(list,ignore_types=(str)): 
for item in list:
    if isinstance(item, Iterable) and not isinstance(item, ignore_types):
        yield from single_list(item,ignore_types=(str))
    else:
        yield item

Item_list = [10,20,[30,40],[50,'Null',70],100]
items_single=single_list(Item_list)
for item in items_single:
    print(item)