0% found this document useful (0 votes)
25 views

List Operations in Python 15 Awesome List Operations in Python

The document provides an introduction to list operations in Python, detailing various methods such as append(), extend(), insert(), remove(), and pop(), among others. Each operation is explained with code examples and expected outputs, illustrating how to manipulate lists effectively. The conclusion emphasizes the importance of mastering these operations for efficient coding in Python.

Uploaded by

aa4048753
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

List Operations in Python 15 Awesome List Operations in Python

The document provides an introduction to list operations in Python, detailing various methods such as append(), extend(), insert(), remove(), and pop(), among others. Each operation is explained with code examples and expected outputs, illustrating how to manipulate lists effectively. The conclusion emphasizes the importance of mastering these operations for efficient coding in Python.

Uploaded by

aa4048753
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Me

nu

.Net Tutorial Alternatives Tutorial Angular Tutorial

Home Software Development Software Development


Tutorials Python Tutorial List Operations in
Python

List Operations in
Python
By Priya Pedamkar !
! !
!

Introduction to List
Operations in Python

List is a type of data structuring method


that allows storing of the integers or the
characters in an order indexed by starting
from 0. List operations are the operations
that can be performed on the data in the
list data structure. A few of the basic list
operations used in Python programming
are extend(), insert(), append(),
remove(), pop(), slice, reverse(), min() &
max(), concatenate(), count(), multiply(),
sort(), index(), clear(), etc.

myList = [1, 2, 3, 'EduCBA', 'makes l

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:

myList.remove('makes learning fun!')


myList.insert(4, 'makes')
myList.insert(5, 'learning')
myList.insert(6, 'so much fun!')
print(myList)

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:

print(myList[:4]) # prints from begi


print(myList[2:]) # prints from star
print(myList[2:4]) # prints from star
print(myList[:]) # prints from begi

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:

print(myList[::-1]) # does not modif


myList.reverse() # modifies the o
print(myList)

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:

9. min() & max()


The min() method returns the minimum
value in the list. The max() method
returns the maximum value in the list.
Both the methods accept only
homogeneous lists, i.e. lists having
elements of similar type.

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:

yourList = [4, 5, 'Python', 'is fun!'


print(myList+yourList)

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

! Popular Course in this category

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

Programming Languages Training (41


Courses, 13+ Projects, 4 Quizzes)!
! 4.9

ANGULAR JS Certification Course!


! 4.8

About Us

Blog

Who is EDUCBA?

Sign Up

Live Classes

Corporate Training

Certificate from Top Institutions

Contact Us

Verifiable Certificate

Reviews

Terms and Conditions

Privacy Policy

Apps

iPhone & iPad

Android

Resources

Free Courses

Java Tutorials

Python Tutorials

All Tutorials

Certification Courses

All Courses

Software Development Course - All in


One Bundle

Become a Python Developer

Java Course

Become a Selenium Automation Tester

Become an IoT Developer

ASP.NET Course

VB.NET Course

PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE


CERTIFICATION NAMES ARE THE TRADEMARKS
OF THEIR RESPECTIVE OWNERS.
Live Chat

You might also like