Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flatten list sublists
#1
Hi guys! I have this list:

 mylist = ["Hello", ["Hello2", "Hello3"], "Hello4"]
However, I want the list to be this instead (don't say just remove the sublist. This is not the real list. Just a test list. The real list comes from a function return so that wouldn't work):

 mylist = ["Hello", "Hello2", "Hello3", "Hello4"]
I have tried this:

sum(map(str.split, sum([[k] if isinstance(k, str) else k for k in single_list1], [])), [])
However this will not work as the real list has spaces and this splits them into different list items too which I do not want.
I have tried a number of different functions in place of str.split however none worked.

Please help
Reply


Messages In This Thread
Flatten list sublists - by chesschaser - Jul-06-2020, 01:07 PM
RE: Flatten list sublists - by snippsat - Jul-06-2020, 01:36 PM
RE: Flatten list sublists - by chesschaser - Jul-06-2020, 01:56 PM
RE: Flatten list sublists - by DeaD_EyE - Jul-06-2020, 02:24 PM
RE: Flatten list sublists - by chesschaser - Jul-06-2020, 02:40 PM
RE: Flatten list sublists - by ndc85430 - Jul-06-2020, 03:11 PM
RE: Flatten list sublists - by chesschaser - Jul-06-2020, 03:41 PM
RE: Flatten list sublists - by rielka - Jul-06-2020, 09:53 PM
RE: Flatten list sublists - by chesschaser - Jul-07-2020, 11:38 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  List of lists - merge sublists with common elements medatib531 1 3,425 May-09-2021, 07:49 AM
Last Post: Gribouillis
  Take first Elements of sublists quest 2 2,256 Apr-26-2021, 11:32 AM
Last Post: buran
  Converting list elements and sublists from int to str iMuny 5 4,605 Mar-10-2019, 09:05 PM
Last Post: iMuny
  Help to flatten list of nested dictionaries shawbapmp 4 5,748 Feb-25-2019, 10:18 PM
Last Post: shawbapmp
  help with flatten nested list Arch2030 6 3,342 Jan-24-2019, 04:50 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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