Python Forum
merging sublist into single list in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
merging sublist into single list in python
#5
Another solution which supports deep nested iterables.

from collections import deque


def extract_words(mixture):
    """
    Flat deep nested iterables and split strings if they occour.
    """
    stack = deque([mixture])
    # using a stack to allow iterating
    # deep nested list
    # it could be done easier with recursion
    # but all stack based languages have a recursion limit
    to_split = (str, bytes)
    # we want to split str and bytes
    while stack:
        # loop runs until stack is empty
        current = stack.popleft()
        # with the first iteration
        # stack is currently empty
        # and current has the first element from stack
        if isinstance(current, to_split):
            # split if the current object is a str or bytes
            yield from current.split()
        else:
            # this branch is executed, if the current object
            # is not a str or bytes
            try:
                current = iter(current)
                # iter of iter returns the same iterator
                subelement = next(current)
                # the next does what the for loop does
            except StopIteration:
                # but we have to check for errors manually
                pass
            except TypeError:
                # and if an element is not iterable, it raieses
                # TypeError. Intgers are for example are not
                # iterable
                yield subelement
            else:
                # if no error happens, put the current iterator back
                # to the left side of the stack
                stack.appendleft(current)
                # put the subelement of the beginning of the deque
                stack.appendleft(subelement)
This is a generator. If you call it, you have to iterate over it.

data = ['Andre Müller', ['hallo', '123'], [[[[['foo bar']]], 'bat']], 12]
extractor = extract_words(data)
# nothing happens, now you can decide if you want to have a list, set, tuple or dict or something different.
result = list(extractor)
# now the generator extractor is exhausted, you can't take it again
# but you can make a new generator and use it for example in a for loop
for element in extract_words(data):
    print(element)
The befit is, that the caller decides which object type is used to put the elements inside.
You can also pipe one generator into the next.

The code above allows also Integers, if you want to filter them, you can make a pipeline.

def filter_non_iterable(iterable):
    allowed = (tuple, list, set, str, bytes)
    for element in iterable:
        if isinstance(element, allowed):
            yield element

filtered = filter_non_iterable(data)
result = extract_words(filtered)
# now still nothing happens


print(list(result))
# now the first generator is consumed by the second generator. The first one filters, the second one does the flatten and split task.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: merging sublist into single list in python - by DeaD_EyE - Mar-22-2019, 02:01 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  J2534 Python Can Bus merging natezoom 0 822 May-01-2023, 10:37 PM
Last Post: natezoom
  python sql query single quote in a string mg24 1 1,215 Nov-18-2022, 08:01 PM
Last Post: deanhystad
  python Multithreading on single file mg24 3 1,940 Nov-05-2022, 01:33 PM
Last Post: snippsat
  Use one list as search key for another list with sublist of list jc4d 4 2,322 Jan-11-2022, 12:10 PM
Last Post: jc4d
Question Sublist/ Subarray into string Python SantiagoPB 2 2,235 Apr-23-2021, 07:03 PM
Last Post: SantiagoPB
  Parse String between 2 Delimiters and add as single list items lastyle 5 3,555 Apr-11-2021, 11:03 PM
Last Post: lastyle
  convert List with dictionaries to a single dictionary iamaghost 3 3,015 Jan-22-2021, 03:56 PM
Last Post: iamaghost
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 2,441 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92
  Undo interation to make a single list? DustinKlent 2 2,270 Nov-29-2020, 03:41 AM
Last Post: DustinKlent
  unique (single) value in dict (or list) 3Pinter 5 2,632 Mar-27-2020, 12:55 PM
Last Post: 3Pinter

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020