python - Replacing one item in a list with two items -


what simplest method replacing 1 item in list two?

so:

list=['t' , 'r', 'g', 'h', 'k'] 

if wanted replace 'r' 'a' , 'b':

list = ['t' , 'a' , 'b', 'g', 'h', 'k'] 

it can done slice assignment:

>>> l = ['t' , 'r', 'g', 'h', 'k'] >>>  >>> pos = l.index('r') >>> l[pos:pos+1] = ('a', 'b') >>>  >>> l ['t', 'a', 'b', 'g', 'h', 'k'] 

also, don't call variable list, since name used built-in function.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -