0

I have a list like the following:

my_list = ['google', 'microsoft,facebook']

and I need to convert this into:

my_list = ['google', 'microsoft', 'facebook']

I tried doing it this way:

new_list = [item.split(',') for item in my_list]
flat_list = [item for sublist in new_list for item in sublist]

but this method involves unnecessary additional variables and multiple for loops. Is there a way we can make this easier?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Vishnukk
  • 524
  • 2
  • 11
  • 27

5 Answers5

4

The below is simple enough (make en empty list and extend it)

my_list = ['google','microsoft,facebook']
new_list = []
for x in my_list:
  new_list.extend(x.split(','))
print(new_list)

output

['google', 'microsoft', 'facebook']
balderman
  • 22,927
  • 7
  • 34
  • 52
3

You can do this by converting to the list to string and back to list as follows:

my_list = ['google','microsoft,facebook']

newList = ','.join(my_list).split(',')

Output:

['google', 'microsoft', 'facebook']
Linda Goba
  • 76
  • 1
  • 4
1

You can do this in one nested comprehension:

new_list = [x for item in my_list for x in item.split(',')]

Or use itertools.chain:

from itertools import chain

new_list = [*chain(*(item.split(',') for item in my_list))]
# or the more traditional
new_list = list(chain.from_iterable(item.split(',') for item in my_list))

And if you want to go all out with the functional utils:

from functools import partial

new_list = [*chain(*map(partial(str.split, sep=","), my_list))]
user2390182
  • 72,016
  • 6
  • 67
  • 89
1

If you insist on doing it in-place, then you can do

for i,e in enumerate(my_list):
    my_list[i:i+1] = e.split(",")
BoarGules
  • 16,440
  • 2
  • 27
  • 44
0

You can flat the list with sum(mylist, [])

l = ['google','microsoft,facebook']

ll = sum([s.split(',')  for s in l], [])

print(ll)

Output

['google', 'microsoft', 'facebook']
cards
  • 3,936
  • 1
  • 7
  • 25
  • 2
    It is generally a bad idea to use `sum` to flatten a list. It has quadratic complexity as it builds ever growing new intermediate list objects. – user2390182 Oct 08 '21 at 07:43
  • @schwobaseggl the docs mentions some disadvantage only for string concatenation: __The preferred, fast way to concatenate a sequence of strings__ which is not the case of this question. See [docs](https://docs.python.org/3.9/library/functions.html#sum) – cards Oct 08 '21 at 07:48
  • The same holds for lists. Which is mentioned in the docs (*"a series of iterables"*). See e.g. [the graph in this nice answer](https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-a-list-of-lists#answer-40813764) – user2390182 Oct 08 '21 at 07:51
  • thanks for your remarks. I always ask myself on how should be interpreted the __consider to__ part of __To concatenate a series of iterables, consider using itertools.chain().__ – cards Oct 08 '21 at 08:01
  • The docs mention strings explicitly because they have their own function (producing a string again, not just a lazy iterator). But the performance issues of `sum` for sequence-like objects (str, list, tuple, ...) are always the same. – user2390182 Oct 08 '21 at 08:03