Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List of List into List
#1
How do you make this list ['Tokyo','London',['Paris','Madrid']] into this list ['Tokyo','London','Paris','Madrid']?
Reply
#2
Taking a nested data structure, with multiple levels, and turning it into a single level, is known as "flattening".  There's many ways to do it, recursion is (I think) the way that's normally suggested, that way it can handle a list within a sublist within the list.

Maybe like this?
>>> cities = ["Tokyo", "London", ["Paris", "Madrid"]]
>>> def flatten(items):
...   for item in items:
...     if isinstance(item, list):
...       yield from flatten(item)
...     else:
...       yield item
...
>>> list(flatten(cities))
['Tokyo', 'London', 'Paris', 'Madrid']
Reply
#3
It would be nice if you provide more information on how do you get this structure, i.e. it may be a problem with how you create it in the first place.
Reply
#4
I was just practicing some stuff on list comprehensions and wanted to see how you go about using list comprehensions to create a single list from a list of list where at least one of the elements in the list is not necessarily an element within a sub-list

For example, it was easy for me to create a single list using list comprehension from the following list [['Tokyo','London'],['Madrid','Paris']], you could simply do:

>>>oList= [['Tokyo','London'],['Madrid','Paris']]
>>>newList = [item for sublist in oList for item in sublist]
>>>newList
['Tokyo', 'London', 'Madrid', 'Paris']
but how would you go about using list comprehensions to create a single list when you have a list like this ['Tokyo','London',['Madrid','Paris']] - makes the problem a little more difficult because using the above approach yields the following result (which is wrong):

>>> oList = ['Tokyo','London',['Madrid','Paris']]
>>> newList = [item for sublist in oList for item in sublist]
>>> newList
['T', 'o', 'k', 'y', 'o', 'L', 'o', 'n', 'd', 'o', 'n', 'Madrid', 'Paris']
I hope that clarifies my problem and would appreciate it if anyone out there knows how to solve this using list comprehensions.
Thank You.
Reply
#5
The first step, is getting all the items to be the same:
>>> oList = ['Tokyo','London',['Madrid','Paris']]
>>> [item if isinstance(item, list) else [item] for item in oList]
[['Tokyo'], ['London'], ['Madrid', 'Paris']]
From there, you can use what you already have:
>>> oList = ['Tokyo','London',['Madrid','Paris']]
>>> [subitem for item in [x if isinstance(x, list) else [x] for x in oList] for subitem in item]
['Tokyo', 'London', 'Madrid', 'Paris']
Reply
#6
more examples
newList = [item for items in oList for item in [[items], items][isinstance(items, list)]]
newList = [item for items in oList for item in (items if isinstance(items, list) else [items])]
is_list = lambda l: l if isinstance(l, list) else [l]
newList = [item for items in oList for item in is_list(items)]
99 percent of computer problems exists between chair and keyboard.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Eight Queens Problem, error in printing list snl_9527 8 487 Apr-27-2025, 11:48 AM
Last Post: Gribouillis
  how to get qualified objects in a list Azdaghost 5 535 Apr-22-2025, 06:03 PM
Last Post: Azdaghost
  Basic list question sanora600 2 508 Apr-13-2025, 07:07 AM
Last Post: noisefloor
  Entry field random pull from list, each return own line Bear1981 6 842 Feb-25-2025, 06:09 AM
Last Post: Pedroski55
  removing one list element without using its index paul18fr 7 1,231 Feb-22-2025, 07:59 PM
Last Post: DeaD_EyE
  question about changing the string value of a list element jacksfrustration 4 2,170 Feb-08-2025, 07:43 AM
Last Post: jacksfrustration
  knowing for loop position in a list medic5678 4 723 Jan-31-2025, 04:19 PM
Last Post: perfringo
  help to parser arguments list absolut 3 699 Jan-15-2025, 03:19 AM
Last Post: BashBedlam
  Get an FFMpeg pass to subprocess.PIPE to treat list as text file? haihal 2 1,075 Nov-21-2024, 11:48 PM
Last Post: haihal
  List comprehension not working right Cris9855 3 969 Nov-04-2024, 03:46 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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