How to Use List insert() Method in PythonPython Basics


The insert() method inserts an element at a specified index in the list.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

list1 = ['MIT', 'CIT', 'Yale','UCI']

list1.insert(1, 'google')

print("the new list: ", list1)

Output:

the new list:  ['MIT', 'google', 'CIT', 'Yale', 'UCI']

Syntax

list.insert(index, element)

Parameters

Name Description
index The position where an element needs to be inserted.
element The element to be inserted in the list.

Return Value

It doesn’t return any value.

© 2024 Learnwithgpt.org, all rights reserved. Privacy Policy | Contact Us