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
Post a Comment