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.
Christian dean
source share