How to replace an element of a list of lines with text separators with multiple list elements in a python list?

Given the list:

mylist = ['dog', 'cat', 'mouse_bear', 'lion_tiger_rabbit', 'ant'] 

I would like a single-line font to return a new list:

 ['dog', 'cat', 'mouse', 'bear', 'lion', 'tiger', 'rabbit', 'ant'] 
+7
python
source share
11 answers

Another trick is to first join the list with underscores, and then split it again:

 "_".join(mylist).split('_') 
+10
source share

Just use 2 for sentences in your understanding, for example:

 >>> mylist = ['dog', 'cat', 'mouse_bear', 'lion_tiger_rabbit', 'ant'] >>> [animal for word in mylist for animal in word.split('_')] ['dog', 'cat', 'mouse', 'bear', 'lion', 'tiger', 'rabbit', 'ant'] 
+5
source share

Python 3.3 ( yield from in list comprehension):

 In [137]: list([(yield from x.split('_')) for x in l]) Out[137]: ['dog', 'cat', 'mouse', 'bear', 'lion', 'tiger', 'rabbit', 'ant'] 
+4
source share

using the itertools recipe to flatten the list, you could do this:

 from itertools import chain mylist = ['dog', 'cat', 'mouse_bear', 'lion_tiger_rabbit', 'ant'] new_list = list(chain.from_iterable(item.split('_') for item in mylist)) print(new_list) # ['dog', 'cat', 'mouse', 'bear', 'lion', 'tiger', 'rabbit', 'ant'] 

... or does the import operator violate your requirements for a single insert?

+3
source share

Since so many answers have been posted (over ten), I thought it would be useful to show some statistics of the time in order to compare the various published methods:

 ----------------------------------------- AChampion time: 2.6322 ----------------------------------------- hiro_protagonist time: 3.1724 ----------------------------------------- Eugene_Sh time: 1.0108 ----------------------------------------- cα΄ΚŸα΄…sα΄˜α΄‡α΄‡α΄… time: 3.5386 ----------------------------------------- jdehesa time: 2.9406 ----------------------------------------- mogga time: 3.1645 ----------------------------------------- Ajax1234 time: 2.4659 ----------------------------------------- 

Here is the script I used for testing:

 from timeit import timeit setup = """ from itertools import chain mylist = ['dog', 'cat', 'mouse_bear', 'lion_tiger_rabbit', 'ant'] """ methods = { 'AChampion': """[animal for word in mylist for animal in word.split('_')]""", 'hiro_protagonist': """list(chain.from_iterable(item.split('_') for item in mylist))""", 'Eugene_Sh': """'_'.join(mylist).split('_')""", 'cα΄ΚŸα΄…sα΄˜α΄‡α΄‡α΄…': """list([(yield from x.split('_')) for x in mylist])""", 'jdehesa': """sum((s.split("_") for s in mylist), [])""", 'mogga': """[i for sublist in [j.split('_') for j in mylist] for i in sublist]""", 'Ajax1234': """list(chain(*[[i] if "_" not in i else i.split("_") for i in mylist]))""" } print('-----------------------------------------') for author, method in methods.items(): print('{} time: {}'.format(author, round(timeit(setup=setup, stmt=method), 4))) print('-----------------------------------------') 

Each method is checked against a sample list asked about a million times in the question. To maintain readability, each synchronization result was rounded to four decimal places.


Note. If you have a new unique method that has not yet been posted here, contact me in the comments and I will try to add time for this too.

+3
source share

Separate each item in the sublists and flatten them:

[item for sublist in mylist for item in sublist.split("_")]

+1
source share

You can do:

 mylist = ['dog', 'cat', 'mouse_bear', 'lion_tiger_rabbit', 'ant'] result = sum((s.split("_") for s in mylist), []) print(result) >>> ['dog', 'cat', 'mouse', 'bear', 'lion', 'tiger', 'rabbit', 'ant'] 
0
source share

It works:

 [i for sublist in [j.split('_') for j in mylist] for i in sublist] 
0
source share

You can try the following:

 from itertools import chain mylist = ['dog', 'cat', 'mouse_bear', 'lion_tiger_rabbit', 'ant'] new_list = list(chain(*[[i] if "_" not in i else i.split("_") for i in mylist])) 

Output:

 ['dog', 'cat', 'mouse', 'bear', 'lion', 'tiger', 'rabbit', 'ant'] 
0
source share
 mylist = ['dog', 'cat', 'mouse_bear', 'lion_tiger_rabbit', 'ant'] animals = [a for item in mylist for a in item.split('_')] print (animals) 
0
source share

what would I actually do:

 newlist = [] for i in mylist: newlist += i.split('_') 
-one
source share

All Articles