

- 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 append objects in a list in Python?
To add a single element to a list in Python at the end, you can simply use the append() method. 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(8) print(my_list)
Output
This will give the output −
[2, 3, 1, -4, -1, -4, 8]
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]
You can also concatenate 2 lists using the extend method. It accepts one list and adds all objects of that list to the end of the list on which it is called.
example
my_list = [2, 3, 1, -4, -1, -4] another_list = ["a", "b"] my_list.extend(another_list) print(my_list)
Output
This will give the output −
[2, 3, 1, -4, -1, -4, "a", "b"]
- Related Questions & Answers
- How to append a list to a Pandas DataFrame using append() in Python?
- How to append list to second list (concatenate lists) in Python?
- How to append element in the list using Python?
- How to shuffle a list of objects in Python?
- How to append a list to a Pandas DataFrame using loc in Python?
- How to append a list to a Pandas DataFrame using iloc in Python?
- How to reverse the objects in a list in Python?
- How to sort the objects in a list in Python?
- How to append a list as a row to a Pandas DataFrame in Python?
- How to append a second list to an existing list in C#?
- Python – Append List every Nth index
- How to append elements in Python tuple?
- How to access Python objects within objects in Python?
- Append list of dictionaries to an existing Pandas DataFrame in Python
- How to open a file in append mode with Python?
Advertisements