- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Add the element in a Python list with help of indexing
A python list is a collection data type that is ordered and changeable. Also, it allows duplicate members. It is the most frequently used collection data type used in Python programs. We will see how we can add an element to a list using the index feature.
But before adding the element in an existing link, let's access the elements in a list using the index feature.
Accessing List using Index
every element in the list is associated with an index and that is how the elements remain ordered. We can access the elements by looping through the indexes. The below program prints the elements at indices 1 and 2. The last index value is not included in the search.
Example
vowels_list = ['a','i','o', 'u'] print(vowels_list[1:3])
Running the above code gives us the following result:
['i', 'o']
Adding Element
We can also use the same concept to add the element at a particular index. In the below program we are adding the element at index position 1.
Example
vowels = ['a','i','o', 'u'] print("values before indexing are :",vowels) #index vowels.insert(1, 'e') print('\nafter adding the element at the index 1 is : ', vowels)
Running the above code gives us the following result:
values before indexing are : ['a', 'i', 'o', 'u'] after adding the element at the index 1 is : ['a', 'e', 'i', 'o', 'u']
- Related Articles
- Python Indexing a sublist
- Add list elements with a multi-list based on index in Python
- Boolean Indexing in Python
- Python – Append given number with every element of the list
- Element with largest frequency in list in Python
- What is a Negative Indexing in Python?
- Removing Array Element and Re-Indexing in PHP
- How to display a list of plots with the help of grid.arrange in R?
- Program to count number of elements present in a set of elements with recursive indexing in Python
- How to find the element from a Python list with a maximum value?
- How to find the element from a Python list with a minimum value?
- How to get the last element of a list in Python?
- Get first element with maximum value in list of tuples in Python
- How to add a new value to each element of list in R?
- How do you add an element to a list in Java?
