Inserting into a list

Items can be inserted into lists using the insert() method, which accepts the index of the new item and the new item itself:

>>> a = 'I accidentally the whole universe'.split()
>>> a
['I', 'accidentally', 'the', 'whole', 'universe']
>>> a.insert(2, "destroyed")
>>> a
['I', 'accidentally', 'destroyed', 'the', 'whole', 'universe']
>>> ' '.join(a)
'I accidentally destroyed the whole universe'

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset