This handles both of your cases, and I think will solve the general case, without any for loops:
def flatten(S):
if S == []:
return S
if isinstance(S[0], list):
return flatten(S[0]) + flatten(S[1:])
return S[:1] + flatten(S[1:])
More Related Contents:
- Find the sum of two lists of lists element-wise [closed]
- List comprehension on a nested list?
- Extract first item of each sublist
- Python recursion with list returns None [duplicate]
- Basics of recursion in Python
- How to do a recursive sub-folder search and return files in a list?
- Finding max value in the second column of a nested list?
- Function with varying number of For Loops (python)
- Search for a key in a nested Python dictionary
- How to find common elements in list of lists?
- Confusing […] List in Python: What is it?
- Change values in dict of nested dicts using items in a list?
- Generate all possible lists of length N that sum to S in Python
- Comprehension on a nested iterables?
- Is there a recursive version of the dict.get() built-in?
- How can I use list comprehensions to process a nested list?
- Turning a list into nested lists in python
- Python recursive function to display all subsets of given set
- How do I reverse a list using recursion in Python?
- Counting depth or the deepest level a nested list goes to
- How to zip two lists of lists
- Given a list of elements in lexicographical order (i.e. [‘a’, ‘b’, ‘c’, ‘d’]), find the nth permutation – Average time to solve?
- How to merge lists into a list of tuples?
- Pythonic way to check if a list is sorted or not
- Python – use list as function parameters
- python recursion return None type
- Append integer to beginning of list in Python [duplicate]
- Iterate a list with indexes [duplicate]
- How to extract all values from a dictionary in Python?
- Comparing 2 lists consisting of dictionaries with unique keys in python