List Operations in Python 15 Awesome List Operations in Python
List Operations in Python 15 Awesome List Operations in Python
nu
List Operations in
Python
By Priya Pedamkar !
! !
!
Introduction to List
Operations in Python
List Operations in
Python
Given below are some of the most widely
used list operations in Python:
1. append()
The append() method is used to add
elements at the end of the list. This
method can only add a single element at
a time. To add multiple elements, the
append() method can be used inside a
loop.
Code:
myList.append(4)
myList.append(5)
myList.append(6)
for i in range(7, 9):
myList.append(i)
print(myList)
Output:
2. extend()
The extend() method is used to add more
than one element at the end of the list.
Although it can add more than one
element, unlike append(), it adds them at
the end of the list like append().
Code:
myList.extend([4, 5, 6])
for i in range(7, 9):
myList.append(i)
print(myList)
Output:
3. insert()
The insert() method can add an element
at a given position in the list. Thus, unlike
append(), it can add elements at any
position, but like append(), it can add
only one element at a time. This method
takes two arguments. The first argument
specifies the position, and the second
argument specifies the element to be
inserted.
Code:
myList.insert(3, 4)
myList.insert(4, 5)
myList.insert(5, 6)
print(myList)
Output:
4. remove()
The remove() method is used to remove
an element from the list. In the case of
multiple occurrences of the same
element, only the first occurrence is
removed.
Code:
Output:
5. pop()
The method pop() can remove an
element from any position in the list. The
parameter supplied to this method is the
index of the element to be removed.
Code:
myList.pop(4)
myList.insert(4, 'makes')
myList.insert(5, 'learning')
myList.insert(6, 'so much fun!')
print(myList)
Output:
6. slice
The slice operation is used to print a
section of the list. The slice operation
returns a specific range of elements. It
does not modify the original list.
Code:
Output:
7. reverse()
The reverse() operation is used to
reverse the elements of the list. This
method modifies the original list. To
reverse a list without modifying the
original one, we use the slice operation
with negative indices. Specifying
negative indices iterates the list from the
rear end to the front end of the list.
Code:
Output:
8. len()
The len() method returns the length of
the list, i.e. the number of elements in the
list.
Code:
print(len(myList))
Output:
Code:
print(min(myList))
Output:
Code:
print(min([1, 2, 3]))
print(max([1, 2, 3]))
Output:
10. count()
The function count() returns the number
of occurrences of a given element in the
list.
Code:
print(myList.count(3))
Output:
11. concatenate
The concatenate operation is used to
merge two lists and return a single list.
The + sign is used to perform the
concatenation. Note that the individual
lists are not modified, and a new
combined list is returned.
Code:
Output:
12. multiply
Python also allows multiplying the list n
times. The resultant list is the original list
iterated n times.
Code:
print(myList*2)
Output:
13. index()
The index() method returns the position
of the first occurrence of the given
element. It takes two optional parameters
– the beginning index and the end index.
These parameters define the start and
end position of the search area on the
list. When supplied, the element is
searched only in the sub-list bound by
the begin and end indices. When not
supplied, the element is searched in the
whole list.
Code:
print(myList.index('EduCBA'))
print(myList.index('EduCBA', 0, 2))
Output:
14. sort()
The sort method sorts the list in
ascending order. This operation can only
be performed on homogeneous lists, i.e.
lists having elements of similar type.
Code:
yourList = [4, 2, 6, 5, 0, 1]
yourList.sort()
print(yourList)
Output:
15. clear()
This function erases all the elements
from the list and empties them.
Code:
myList.sort()
print(myList)
Output:
Conclusion
List operations are strategically essential
to learn how to perform a task with
minimal lines of code. This is just a basic
introduction to list operations covering
most of the methods. It is recommended
to play around more, get creative and
explore the potential of lists further.
Recommended
Articles
We hope that this EDUCBA information
on “List Operations in Python” was
beneficial to you. You can view EDUCBA’s
recommended articles for more
information.
1. Advantages of Python
2. Star Patterns in Python
3. Boolean Operators in Python
4. Palindrome in Python
PYTHON - Everything in
PYTHON
!
! 362+ Hours of HD Videos | !
! 81 Courses |
!
! 59 Mock Tests & Quizzes | !
! Verifiable
Certificate of Completion
!
!!!!
!!!!
! 4.8
Price
₹3999
View Course
Related Courses
About Us
Blog
Who is EDUCBA?
Sign Up
Live Classes
Corporate Training
Contact Us
Verifiable Certificate
Reviews
Privacy Policy
Apps
Android
Resources
Free Courses
Java Tutorials
Python Tutorials
All Tutorials
Certification Courses
All Courses
Java Course
ASP.NET Course
VB.NET Course
PHP Course