Lists
Lists
[0] 10 [-5] 10
[1] 20 [-4] 20
a [2] -50 a [-3] -50
[3] 21.3 [-2] 21.3
[4] Geekyshows [-1] Geekyshows
Accessing List’s Element
a = [10, 20, -50, 21.3, ‘Geekyshows’]
print(a[0])
print(a[1]) 10 20 -50 21.3 Geekyshows
a[0] a[1] a[2] a[3] a[4]
print(a[2])
print(a[3])
print(a[4])
Modifying or Updating Element
Lists are mutable so we can modify it’s element.
a = [10, 20, -50, 21.3, ‘Geekyshows’]
a[1] = 40
10 20
40 -50 21.3 Geekyshows
a[0] a[1] a[2] a[3] a[4]
Accessing using for loop
a = [10, 20, -50, 21.3, ‘Geekyshows’]
Without index
for element in a:
print(element)
With index
n = len(a)
for i in range(n):
print(a[i])
Accessing using while loop
a = [10, 20, -50, 21.3, ‘Geekyshows’]
n = len(a)
i=0
while i < n :
print(a[i])
i+=1
Deletion
del statement is used to delete an element of list or we can delete entire list
using del statement.
a = [10, 20, -50, 21.3, ‘Geekyshows’]
Deleting Element
del a[2]
print("List:")
for element in a:
print (element)
insert( )
This method is used to insert an element in a particular position of the existing
list.
Syntax:-
list_name.insert(position_number, new_element)
pop ( )
This method is used to remove last element from the existing list.
Syntax:-
list_name.pop( )
pop (n)
This method is used to remove an element specified by position number, from
the existing list and returns removed element.
Syntax:-
list_name.pop(position_number)
remove( )
This method is used to remove first occurrence of given element from the
existing list. If it doesn’t found the element, shows valueError.
Syntax:-
list_name.remove(element)
index( )
This method returns position number of first occurrence of given element in
the list. If it doesn’t found the element, shows valueError.
Syntax:-
list_name.index(element)
reverse ( )
This method is used to reverse the order of elements in the list.
Syntax:-
list_name.reverse( )
extend( )
This method is used to append another list or iterable object at the end of the
list.
Syntax:-
list_name.extend(lst)
count( )
This method returns number of occurrence of a specified element in the list.
Syntax:-
list_name.count(specified_element)
sort( )
This method is used to sort the elements of the list into ascending order.
Syntax:-
list_name.sort()
clear( )
This method is used to delete all the elements from the list
Syntax:-
list_name.clear()
Slicing on List
Slicing on list can be used to retrieve a piece of the list that contains a group
of elements. Slicing is useful to retrieve a range of elements.
Syntax:-
new_list_name = list_name[start:stop:stepsize]
List Concatenation
+ operator is used to do concatenation the list.
Ex:-
a = [10, 20, 30]
b = [1, 2, 3]
result = a + b
print(result)
Repetition of List
* Operator is used to repeat the elements of list.
Ex:-
b = [1, 2, 3]
result = b * 3
print(result)
Aliasing List
Aliasing means giving another name to the existing object. It doesn’t mean
copying. a
a = [10, 20, 30, 40, 50]
b=a 10 20 30 40 50
Modification in a will [0] [1] [2] [3] [4]
affect b and vice versa.
b
10 20 30 40 50
Copying List
copy( ) method is used to copy all the elements of a list to another list.
When we copy a list a separate copy of all the elements is stored in another
list. Both the list are independent. a
a = [10, 20, 30, 40, 50]
b = a.copy() 10 20 30 40 50
[0] [1] [2] [3] [4]
Modification in a will not
affect b and vice versa. b
10 20 30 40 50
[0] [1] [2] [3] [4]
Cloning List
We can clone a list into another list using slicing.
When we clone a list a separate copy of all the elements is stored in another
list. Both the list are independent.
a
a = [10, 20, 30, 40, 50]
b = a[:]
10 20 30 40 50
[0] [1] [2] [3] [4]
Modification in a will not
affect b and vice versa. b
10 20 30 40 50
[0] [1] [2] [3] [4]