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
#4
@snippsat, unfortunately the OP has mixture of strings and nested lists of strings [ 'New', ['Group Name Details'], ['Group Name'], ...], so invoking line.split(), when line is a list, will lead to an error. I rewrite my solution to be more pythonic:

def extract_words(mixture):
    """Extract words from mixture of words and lists of words.
    """

    result = list()
    for item in mixture:
        if isinstance(item, list):
            for _astring in item:
                result += _astring.split()
        else:
            result += item.split()
    return result
Reply


Messages In This Thread
RE: merging sublist into single list in python - by scidam - Mar-22-2019, 12:03 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