insert() in Python

Configurare noua (How To)

Situatie

insert() is an inbuilt function in Python that inserts a given element at a given index in a list.

Syntax: list_name.insert(index, element)

Solutie

Pasi de urmat

 

list1 = [ 1, 2, 3, 4, 5, 6, 7 ]

list1.insert(4, 10)
print(list1)

list2 = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

list2.insert(0, ‘z’)
print(list2)

 

Output:

[1, 2, 3, 4, 10, 5, 6, 7]
[‘z’, ‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

Tip solutie

Permanent

Voteaza

(1 din 5 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?