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
  element in list detection problem jacksfrustration 5 408 Apr-11-2024, 05:44 PM
Last Post: deanhystad
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 289 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  PyYAML read list of int zisco 2 325 Apr-02-2024, 12:36 PM
Last Post: zisco
  How to parse and group hierarchical list items from an unindented string in Python? ann23fr 0 206 Mar-27-2024, 01:16 PM
Last Post: ann23fr
  list.sort() returning None SmallCoder14 8 607 Mar-19-2024, 09:49 PM
Last Post: SmallCoder14
  append str to list in dataclass flash77 6 498 Mar-14-2024, 06:26 PM
Last Post: flash77
  Help with to check an Input list data with a data read from an external source sacharyya 3 421 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  list and operator * akbarza 1 267 Feb-29-2024, 08:10 AM
Last Post: DeaD_EyE
  difference between forms of input a list to function akbarza 6 1,046 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  using > < for tuple , list,... akbarza 3 487 Feb-05-2024, 01:18 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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