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?

1
  • 1
    @SatyamShankar Look more closely! Commented Oct 8, 2021 at 7:34

5 Answers 5

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']
Sign up to request clarification or add additional context in comments.

Comments

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']

Comments

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))]

Comments

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(",")

Comments

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']

5 Comments

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.
@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
The same holds for lists. Which is mentioned in the docs ("a series of iterables"). See e.g. the graph in this nice answer
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().
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.