‘is0i2t, 4:43 PM DDayS_List Methods -Jupyter Notebook
In [ J:
Python List Methods
Python has many useful list methods that makes it really easy to work with lists.
Here are some of the commonly used list methods.
Methods Descriptions
append() adds an element to the end of the list
extend() adds all elements of a list to another list
insert() inserts an item at the defined index
remove() removes an iten from the list
pop() returns and removes an element at the given index
clear() removes all items from the list
index() returns the index of the first matched item
count() returns the count of the number of items passed as an argument
sort() sort items in a list in ascending order
reverse() reverse the order of items in the list
copy() returns a shallow copy of the list
< »
In [4]:
1a = [10,20]
12 = [30,40]
l1.extend(12)
a
out [4]:
[10, 2, 30, 40]
In [ ]:
insert:
The insert() method can add an element at a given position in the list.
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 elemen
to be inserted.
In [2]:
myList = [10,20,30]
myList.insert(3, 4)
myList.insert(4, 5)
myList.insert(s, 6)
print (myList)
[10, 20, 30, 4, 5, 6]
localhost 8888/notebocksiDayS._List Methods ipynt am‘wra0r2t, 4:43 PM DayS_List Mathods - Jupyter Notebook
In{ J:
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.
In [6]:
myList.append(4e)
myList
out[6]:
[10, 20, 30, 4, 5, 6, 40]
In [9]:
d
[10, 2, 30, 4, 5, 6, 40,4,4,4]
In [10]:
a
out[10]:
(10, 28, 38, 4, 5, 6, 40, 4, 4, 4]
In [11]:
d.remove(4)
In [12]:
a
out[12]:
[10, 20, 30, 5, 6, 40, 4, 4, 4]
In [7]:
d
out [7]:
[10, 2, 30, 5, 6, 40, 4, 4, 4)
In [ ]:
localhost 8888/notebocksiDayS._List Methods ipynt
am‘wra0r21, 4:43 PM DayS_List Mathods - Jupyter Notebook
In [11]:
d
out (11):
[1@, 20, 30, 5, 6, 40, 4, 4, 4)
In [ ]:
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.
In [16]:
out [16]:
[1@, 20, 30, 5, 6, 40, 4, 4]
In [17]:
d.pop(s)
out (17):
40
In [15]:
4.pop(38)
IndexError Traceback (most recent call last)
in
-> 1 d.pop(3@)
IndexError: pop index out of range
In [12]:
myList
out [12]:
[10, 20, 30, 5, 6, 40]
localhost 8888/notebocksiDayS._List Methods ipynt‘wra0r21, 4:43 PM DayS_List Mathods - Jupyter Notebook
In [14]:
myList.pop()
myList
out[14]:
[1@, 20, 38, 5]
In [6]:
myList.pop(2)
myList
out[6]:
[1@, 20, 5, 6)
In[ ]:
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.
In [18]:
out[18]:
[40, 30, 2, 19]
In [19]:
myList
out[19]:
[10, 20, 30, 4, 5, 6]
In [21]:
ia = [1,2,3]
In [22]:
print (14[::-1])
(3, 2, 1)
localhost 8888/notebocksiDayS._List Methods ipynt‘wis0/21, 4:43 PM DDayS_List Methods -Jupyter Notebook
In [23]:
lt
out[23]:
[a 2, 3]
In [20]:
print(myList[::-1]) # does not modify the original List
myList.reverse() # modifies the original List
print (myList)
(6, 5, 4, 30, 2, 10]
(6, 5, 4, 30, 20, 10]
In [ J:
count()
The function count() returns the number of occurrences of a given elenent
in the list.
In [26]:
11 = [10,20,10,20, 30]
print (11.count (20))
In [J]:
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
sturned.
In [23]:
11 = [10,20,30]
12 = [40,50,60]
print(11+12)
(10, 20, 30, 40, 50, 60]
In [25]:
print (12*3)
[40, 50, 60, 42, 50, 60, 40, 50, 60]
localhost 8888/notebocksiDayS._List Methods ipynt
sm‘rao, 443 PM DayS_List Methods - Jupyler Notebook
In [26]:
nh
out [26]:
[10, 20, 30]
In [27]:
2
out[27]:
(40, 50, 60]
In[ ]:
The index() method returns the position of the first occurrence of the given
element.
It takes two optional parameters - the begin index and the end index.
These parameters define the start and end position of the search area on the
list.
In [28]:
11 = [10,20,30,40,50, 60,10, 20,50]
11. index(190)
Valueérror Traceback (most recent call last)
in
1 11 = [10,20,30,40,50,60,10, 20,50]
sss-> 2 11 index(100)
ValueError: 100 is not in list
In [16]:
11. index(1@)
out[16]:
@
In [30]:
un
out[3@]:
[1@, 20, 30, 42, 50, 60, 10, 20, se]
localhost 8888/notebocksiDayS._List Methods ipynt
em‘oor, 449 Pm (DayS_List Mathods -Jupytr Notebook
In [33]:
print (11. index(30,0,3))
In (J:
sort()
The sort method sorts the list in ascending order.
This operation can only be performed on homogeneous lists,
- lists having elements of similar type.
In [29]:
11 = [10,20,30,40,5¢,60,10,20]
la.sort()
a
out[29]:
[18, 10, 20, 20, 30, 40, 58, 60)
In [37]:
s = ['banana', ‘apple’, 'mango'}
s.sort()
s
out[37]:
[‘apple’, ‘banana’, ‘mango’ ]
In [30]:
12 = ['e','a','2','n']
12. sort(reverse=True)
2
out[3@]:
In [20]:
ln
out[2e]:
[10, 18, 20, 20, 30, 40, 50, 60]
localhost 8888/notebocksiDayS._List Methods ipynt
mm‘wra0r21, 4:43 PM
In [31]:
n= input("Enter any number:
print(n)
Enter any number: 1¢
10
In [32]:
print (type(n))
In [ J:
List Comprehension:
DayS_List Mathods - Jupyter Notebook
List comprehension is an elegant and concise way to create a new list
from an existing list
in Python.A list comprehension consists of an expression followed by
for statement inside square brackets.
In [35]:
a = float(input())
b = float(input())
print(a+b)
10
20
30.0
In [37]:
1=()
for i in range(5):
ele = int(input("Enter any number:
L.append(ele)
print (1)
18
20
Enter any nunber:
Enter any number:
Enter any number: 3@
Enter any number: 40
Enter any number: 5@
[10, 20, 30, 40, se]
In []:
1-0]
ele = int(input("Enter any number: "))
while ele:
L.append(ele)
print(1)
localhost 8888/notebocksiDayS._List Methods ipynt
»)
am‘is0i2t, 4:43 PM DDayS_List Methods -Jupyter Notebook
In [41]:
for i in range(10)
print (i,end="
0123456789
In [50]:
11 = [10,20,30]
In [51]:
newlist = [x for x in 11]
tn [52]:
newlist
out [52]:
[1e, 20, 30]
In [61]:
#Example
#Accept only numbers Lower than 5:
newlist = [x for x in range(10) if x%2!=0]
In [62]:
newlist
out[62]:
[1, 3, 5,7, 9]
In [64]:
fruits = ["apple", “banana”, “cherry”, “kiwi", "mango"]
In [65]:
newlist = [x.upper() for x in fruits]
In [67]:
newlist
out[67]:
[‘APPLE', “BANANA', ‘CHERRY’, ‘KIWI, ‘MANGO']
localhost 8888/notebocksiDayS._List Methods ipynt
om‘is0i2t, 4:43 PM DDayS_List Methods -Jupyter Notebook
In [68]:
newlist1 = [x.lower() for x in newlist]
In [69]:
newlist1
out [69]:
['apple', ‘banana', ‘cherry’, ‘kiwi', ‘mango']
in [ ]:
# List comprehension offers a shorter syntax when you want to create
# a new List based on the values of an existing List.
In [70]:
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []
for x in fruits:
af "a" in x:
newlist.append(x)
print(newlist)
['apple’, ‘banana’, ‘mango']
In [ J:
#With List comprehension you can do all that with only one Line of code:
In [71]:
fruits = ["apple", “banana”, “cherry”, “kiwi", "mango"]
newlist = [x for x in fruits if "
in x]
print(newlist)
[‘apple’, ‘banana', ‘mango’ ]
In []:
The Syntax
newlist = [expression for item in iterable if condition
True]
localhost 8888/notebocksiDayS._List Methods ipynt rom‘wra0r21, 4:43 PM DayS_List Mathods - Jupyter Notebook
In [80]:
11 = [x*#3 for x in range(15) if x%2
In [81]:
a
out (81):
[@, 8, 64, 216, 512, 1000, 1728, 2744)
In [3]:
newlist = [x for x in fruits if x
pple")
In [4]:
newlist
out[4]:
[‘banana’, ‘cherry’, 'kiwi', ‘mango’ ]
in [ ]:
localhost 8888/notebocksiDayS._List Methods ipynt
wm