Assuming lyst has the correct number of True and False entries, you can do this efficiently by creating iterators of two other lists. This is more efficient than pop ing from the front lists.
lyst = [True, True, False, True, False] car = ['BMW', 'VW', 'Volvo'] a = ['b', 'c'] selector = [iter(a), iter(car)] result = [next(selector[b]) for b in lyst] print(result)
Output
['BMW', 'VW', 'b', 'Volvo', 'c']
The selector[b] construct works because False has a numeric value of 0 and True has a numeric value of 1, so we can use the logical values of lyst to index into a selector list.
Delays
I decided to write a timeit script to compare the speeds of the various algorithms posted here, as well as the vaultah code from the target error question .
To test these algorithms, I included them in functions. To obtain accurate timeit timing timeit , the function is executed several times. Algorithms that use .pop consume the lists from which they appear, so I had to add code to these functions to create copies of these lists. Obviously, this copying process slows down these functions, and in some applications it may not matter that the input data lists are destroyed, but I believe that copying time should be included in order to sufficiently compare the speeds of these functions for more general use.
timeit measures the time of the wall clock, not the processor time, so other processes running on the system will affect the timings. Therefore, when running the timeit code, timeit recommended to reduce the load on the system as much as possible. Ideally, you should disable network access, or at least avoid using your browser while testing. And definitely don’t listen to music or watch the video. :)
The code below runs on Python 2.6+, as well as Python 3. I tested it on Python 2.6.6 and Python 3.6.0.
Python 3 exit
Testing all functions with the question data Expected: ['BMW', 'VW', 'b', 'Volvo', 'c'] merge_wwii_ifelse True merge_wwii_andor True merge_PM2R_it True merge_PM2R_pop True merge_AlexL True merge_Laurent True merge_Shihab True merge_EvKounis True merge_vaultah True List size = 5, loops = 15625 All functions verified True merge_Shihab [0.1553873940010817, 0.15573736099941016, 0.1570748160011135] merge_vaultah [0.19238512099946092, 0.19444048600053065, 0.19622620799964352] merge_wwii_ifelse [0.1928826420007681, 0.20096588600063114, 0.2038269639997452] merge_wwii_andor [0.19839437199880194, 0.19887267900048755, 0.1997791949997918] merge_PM2R_it [0.21258554000087315, 0.21627756400084763, 0.21734632500010775] merge_Laurent [0.22584542599906854, 0.22584707799978787, 0.2275525179993565] merge_PM2R_pop [0.252919409000242, 0.2563238849998015, 0.28485306599941396] merge_AlexL [0.295069166999383, 0.2970452879999357, 0.3192335510011617] merge_EvKounis [0.3210714779997943, 0.32545770300021104, 0.3283817559986346] List size = 25, loops = 3125 All functions verified True merge_wwii_andor [0.13853849199949764, 0.13861457399980281, 0.16665312800068932] merge_vaultah [0.1385455899999215, 0.13872790400091617, 0.13888424499964458] merge_wwii_ifelse [0.14001116700092098, 0.1411627879988373, 0.14938874099971144] merge_PM2R_pop [0.1415618489991175, 0.14180967100037378, 0.14234910300001502] merge_Shihab [0.1417505749996053, 0.14575945399883494, 0.1469801869989169] merge_PM2R_it [0.15280632599933597, 0.1578172520003136, 0.20611876800103346] merge_Laurent [0.15372244299942395, 0.15373632599948905, 0.15608775699911348] merge_AlexL [0.19783953799924348, 0.19835066099949472, 0.19853855400106113] merge_EvKounis [0.21501469599934353, 0.21525065300011192, 0.21861338300004718] List size = 125, loops = 625 All functions verified True merge_PM2R_pop [0.10481183099909686, 0.10530150200065691, 0.1054428099996585] merge_vaultah [0.12480986199989275, 0.125469589000204, 0.12560611799926846] merge_wwii_andor [0.1248406070008059, 0.1251086979991669, 0.12896737399933045] merge_wwii_ifelse [0.1248461369996221, 0.12487849500030279, 0.1357243729999027] merge_Shihab [0.13901417600027344, 0.14433371599989187, 0.14757765399917844] merge_Laurent [0.14081091899970488, 0.14126963899980183, 0.21554046200071753] merge_PM2R_it [0.14273404000050505, 0.1436745359987981, 0.14957189699998708] merge_AlexL [0.1876233860002685, 0.18891249499938567, 0.2016287040005409] merge_EvKounis [0.20533967399933317, 0.2060009060005541, 0.2169854090006993] List size = 625, loops = 125 All functions verified True merge_PM2R_pop [0.10215267800049332, 0.10283978299958108, 0.10370849799983262] merge_vaultah [0.12245723900014127, 0.1226432970015594, 0.12315383100030886] merge_wwii_ifelse [0.12513774199942418, 0.12616848099969502, 0.12886606600113737] merge_wwii_andor [0.12801914100055, 0.12805822200061812, 0.12933381000038935] merge_PM2R_it [0.137118164999265, 0.13727735000065877, 0.1388498169999366] merge_Laurent [0.138064671000393, 0.1399875509996491, 0.1408491909987788] merge_Shihab [0.14056324699959077, 0.14599118399928557, 0.190011668999432] merge_AlexL [0.2494661869986885, 0.24949409599867067, 0.25023536899971077] merge_EvKounis [0.2604512350008008, 0.2616422920000332, 0.26348238600076] List size = 3125, loops = 25 All functions verified True merge_PM2R_pop [0.10108045999913884, 0.1956245539986412, 0.19625802900009148] merge_vaultah [0.1205880960005743, 0.12112638399958087, 0.12208285199994862] merge_wwii_andor [0.12255161999928532, 0.12333094499990693, 0.12521908300004725] merge_wwii_ifelse [0.12545375700028671, 0.12909694299924013, 0.1335049829995114] merge_PM2R_it [0.13350266700035718, 0.1339889190003305, 0.13768064900068566] merge_Laurent [0.13691045599989593, 0.1544653910004854, 0.15945969300082652] merge_Shihab [0.13996293700074602, 0.14073836100033077, 0.15528064499994798] merge_AlexL [0.5525499420000415, 0.562283696999657, 0.954857885999445] merge_EvKounis [0.5699720739994518, 0.5713485720007156, 0.57778780100125] List size = 15625, loops = 5 All functions verified True merge_PM2R_pop [0.10786392400041223, 0.10926139099865395, 0.10992666599850054] merge_vaultah [0.12552327299999888, 0.1260173209993809, 0.126270750999538] merge_wwii_ifelse [0.13077028499901644, 0.13549192800019227, 0.13690466499974718] merge_wwii_andor [0.13939281700004358, 0.14145590299995092, 0.1771287490009854] merge_Laurent [0.14111154199963494, 0.141571592001128, 0.14596649399936723] merge_PM2R_it [0.14352190899990092, 0.1444555029993353, 0.1446186849989317] merge_Shihab [0.1475988830006827, 0.14831254499949864, 0.15240716699918266] merge_AlexL [2.040518506999433, 2.0523803409996617, 2.1246015890010312] merge_EvKounis [2.0611778700003924, 2.0809960999995383, 2.0933345120010927]
Python 2 exit
Testing all functions with the question data Expected: ['BMW', 'VW', 'b', 'Volvo', 'c'] merge_wwii_ifelse True merge_wwii_andor True merge_PM2R_it True merge_PM2R_pop True merge_AlexL True merge_Laurent True merge_Shihab True merge_EvKounis True merge_vaultah True List size = 5, loops = 15625 All functions verified True merge_wwii_ifelse [0.13266801834106445, 0.13341188430786133, 0.14590716361999512] merge_vaultah [0.1372370719909668, 0.20317387580871582, 0.28184700012207031] merge_wwii_andor [0.1373291015625, 0.14022302627563477, 0.14162182807922363] merge_Shihab [0.14127588272094727, 0.14717292785644531, 0.15103507041931152] merge_PM2R_it [0.16237378120422363, 0.16309309005737305, 0.1668241024017334] merge_Laurent [0.18718504905700684, 0.21074390411376953, 0.24513697624206543] merge_PM2R_pop [0.28445720672607422, 0.29493808746337891, 0.32616496086120605] merge_EvKounis [0.29379916191101074, 0.33054614067077637, 0.34055399894714355] merge_AlexL [0.29791903495788574, 0.29801201820373535, 0.31824994087219238] List size = 25, loops = 3125 All functions verified True merge_vaultah [0.10121989250183105, 0.10190892219543457, 0.10230588912963867] merge_wwii_andor [0.10298919677734375, 0.10324811935424805, 0.1042180061340332] merge_wwii_ifelse [0.10428714752197266, 0.1065058708190918, 0.13893890380859375] merge_PM2R_it [0.11803603172302246, 0.11927890777587891, 0.12329411506652832] merge_Shihab [0.12048506736755371, 0.1209111213684082, 0.14031100273132324] merge_Laurent [0.12093997001647949, 0.12250900268554688, 0.12366700172424316] merge_PM2R_pop [0.14476203918457031, 0.14788198471069336, 0.15009903907775879] merge_AlexL [0.2096550464630127, 0.2101140022277832, 0.21013402938842773] merge_EvKounis [0.22748994827270508, 0.24114799499511719, 0.27338886260986328] List size = 125, loops = 625 All functions verified True merge_wwii_ifelse [0.093290090560913086, 0.095035076141357422, 0.10503697395324707] merge_wwii_andor [0.095438957214355469, 0.096124887466430664, 0.096685171127319336] merge_vaultah [0.096237897872924805, 0.096857070922851562, 0.10109710693359375] merge_Laurent [0.11233901977539062, 0.11253595352172852, 0.11261391639709473] merge_PM2R_it [0.11286282539367676, 0.11314296722412109, 0.11331295967102051] merge_PM2R_pop [0.11631202697753906, 0.11657595634460449, 0.11874699592590332] merge_Shihab [0.12019991874694824, 0.12040495872497559, 0.12069797515869141] merge_AlexL [0.21399688720703125, 0.21718001365661621, 0.23727011680603027] merge_EvKounis [0.22643208503723145, 0.22728395462036133, 0.22952795028686523] List size = 625, loops = 125 All functions verified True merge_wwii_ifelse [0.091418981552124023, 0.091669797897338867, 0.097893953323364258] merge_vaultah [0.092507839202880859, 0.09267878532409668, 0.09857487678527832] merge_wwii_andor [0.094152212142944336, 0.094262123107910156, 0.095048904418945312] merge_PM2R_pop [0.10612797737121582, 0.10874819755554199, 0.15479302406311035] merge_Laurent [0.10751605033874512, 0.10817885398864746, 0.1108400821685791] merge_PM2R_it [0.10856819152832031, 0.10870695114135742, 0.10909485816955566] merge_Shihab [0.12109684944152832, 0.12231802940368652, 0.12318301200866699] merge_AlexL [0.26717281341552734, 0.27173185348510742, 0.27331304550170898] merge_EvKounis [0.27798080444335938, 0.27915000915527344, 0.28347897529602051] List size = 3125, loops = 25 All functions verified True merge_wwii_ifelse [0.089553117752075195, 0.090708017349243164, 0.096336126327514648] merge_wwii_andor [0.092846870422363281, 0.092998027801513672, 0.09307098388671875] merge_vaultah [0.093343973159790039, 0.096785068511962891, 0.11030793190002441] merge_Laurent [0.10524392127990723, 0.10576510429382324, 0.10692310333251953] merge_PM2R_pop [0.10529899597167969, 0.10684394836425781, 0.1188349723815918] merge_PM2R_it [0.1075129508972168, 0.1087181568145752, 0.11004185676574707] merge_Shihab [0.12116694450378418, 0.1221461296081543, 0.12650799751281738] merge_AlexL [0.57863092422485352, 0.58119797706604004, 0.62129878997802734] merge_EvKounis [0.59500694274902344, 0.59542298316955566, 0.59793877601623535] List size = 15625, loops = 5 All functions verified True merge_wwii_ifelse [0.093538999557495117, 0.094748973846435547, 0.099496126174926758] merge_wwii_andor [0.097050189971923828, 0.097167015075683594, 0.14334392547607422] merge_vaultah [0.097479820251464844, 0.097939968109130859, 0.10231399536132812] merge_Laurent [0.10894298553466797, 0.10906291007995605, 0.11107587814331055] merge_PM2R_pop [0.11378717422485352, 0.11389708518981934, 0.11422491073608398] merge_PM2R_it [0.11379504203796387, 0.1164860725402832, 0.11690497398376465] merge_Shihab [0.12729287147521973, 0.12743711471557617, 0.12863397598266602] merge_AlexL [2.062777042388916, 2.0632898807525635, 2.104421854019165] merge_EvKounis [2.0776879787445068, 2.1075038909912109, 2.1273050308227539]
These results were obtained from my rather ancient 32-bit single-core 2GHZ machine with 2 GB of RAM working with the Debian manufacturer of Linux.