4

I want to convert this list

list = ['orange','cherry,strawberry','cucumber,tomato,coconut,avocado','apple','blueberry,banana']

to

new_list = ['orange','cherry','strawberry','cucumber','tomato','coconut','avocado','apple','blueberry','banana']

Is it possible to do that? Please help me!

timgeb
  • 76,762
  • 20
  • 123
  • 145

5 Answers5

5

Join and split!

>>> lst = ['orange','cherry,strawberry','cucumber,tomato,coconut,avocado','apple','blueberry,banana']
>>> ','.join(lst).split(',')
['orange', 'cherry', 'strawberry', 'cucumber', 'tomato', 'coconut', 'avocado', 'apple', 'blueberry', 'banana']
timgeb
  • 76,762
  • 20
  • 123
  • 145
4

If the only rule is that each entry in the list can be a string representing a single item (without commas) or a list of items separated by commas, you can just use str.split(',') on each entry.

Notice the terminology there, it's important:

  • The list is what you have and it contains a number of string entries;
  • Each string entry is either an item or a comma-separated list of items;
  • Each item is a thing you want as a separate entry in the new list.

For an entry without commas, str.split will give you a list with just that one item:

>>> 'cherry'.split(',')
['cherry']

For an item with commas, it will give you a list containing all the items in the entry:

>>> 'cherry,banana'.split(',')
['cherry', 'banana']

So you just need to process the list and, for each entry in the list, split it into an item list and use each of those items to construct a new list:

[item for entry in my_list for item in entry.split(',')]
 |  | \                  / \                          /
 |  |  \ get each entry /   \ get each item of entry /
 |  |   \______________/     \______________________/
 |  |
 +--+------> deliver all items into new list

You can see this working in the following transcript:

>>> my_list = ['orange', 'cherry,strawberry', 'apple', 'blueberry,banana']
>>> [item for entry in my_list for item in entry.split(',')]
['orange', 'cherry', 'strawberry', 'apple', 'blueberry', 'banana']

Just keep in mind that, if you want to handle entries with spaces like 'cherry, pie'. you should probably remove extraneous spaces:

[item.strip() for entry in ...
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
2

You can split on",", then flatten the sub lists with itertools.chain.from_iterable:

>>> from itertools import chain
>>> lst = ['orange','cherry,strawberry','cucumber,tomato,coconut,avocado','apple','blueberry,banana']
>>> list(chain.from_iterable(x.split(",") for x in lst))
['orange', 'cherry', 'strawberry', 'cucumber', 'tomato', 'coconut', 'avocado', 'apple', 'blueberry', 'banana']
RoadRunner
  • 25,803
  • 6
  • 42
  • 75
2

Extend an accumulator list (newvals) while splitting on string-commas (",").

values = ['orange','cherry,strawberry','cucumber,tomato,coconut,avocado','apple','blueberry,banana']

newvals = []
for v in values: newvals.extend(v.split(','))
print(newvals) 

Output:

['orange', 'cherry', 'strawberry', 'cucumber', 'tomato', 'coconut', 'avocado', 'apple', 'blueberry', 'banana']
CypherX
  • 7,019
  • 3
  • 25
  • 37
0

We can also flatten the list as,

list_1 = ['orange','cherry,strawberry','cucumber,tomato,coconut,avocado','apple','blueberry,banana', 2, 2.5]





def to_flatten(my_list, primitives=(bool, str, int, float)):
    
    flatten = []
    my_list = [stuff.split(',') if type(stuff) == str else stuff for stuff in list_1]
    
    for item in my_list:
        if isinstance(item, primitives):
            flatten.append(item)
        else:
            flatten.extend(item)
    return flatten
   
print(to_flatten(list_1))

which gives

['orange', 'cherry', 'strawberry', 'cucumber', 'tomato', 'coconut', 'avocado', 'apple', 'blueberry', 'banana', 2, 2.5]

[Program finished]

you can change primitives as per requirement

Subham
  • 397
  • 1
  • 6
  • 14