

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to insert an object in a list at a given position in Python?
If you want to insert an element at a given position, use the insert(pos, obj) method. It accepts one object and adds that object at the position pos of the list on which it is called.
example
my_list = [2, 3, 1, -4, -1, -4] my_list.insert(1, 0) print(my_list)
Output
This will give the output −
[2, 0, 3, 1, -4, -1, -4]
If you want to insert an object at the end only, then use append instead of insert. It accepts one object and adds that object to the end of the list on which it is called.
example
my_list = [2, 3, 1, -4, -1, -4] my_list.append(0) print(my_list)
Output
This will give the output −
[2, 3, 1, -4, -1, -4, 0]
- Related Questions & Answers
- How to insert an item in a list at a given position in C#?
- How to insert an object in an ArrayList at a specific position in java?
- Insert an element at second position in a C# List
- Delete a Linked List node at a given position in C++
- Delete a Doubly Linked List node at a given position in C++
- Program to insert new element into a linked list before the given position in Python
- Python Pandas - Insert a new index value at a specific position
- C program to insert a node at any position using double linked list
- Insert a character at nth position in string in JavaScript
- How to insert a Python object in Mongodb?
- How to insert a Python object in MySQL?
- Adding an element at a given position of the array in Javascript
- How to remove an object from a list in Python?
- In MySQL, how can we insert a substring at the specified position in a string?
- How to simulate touch event with android at a given position?
Advertisements