Friday 15 May 2020

List Datatype Part 2






Important functions of List:

 

I.  To get information about list:

 1. len(): returns the number of elements present in the list

Eg:

 n=[10,20,30,40]

print(len(n))------>4

 

2. count(): It returns the number of occurrences of specified item in the list

n=[1,2,2,2,2,3,3]

print(n.count(1)) ------> 1

print(n.count(2)) ------> 4

print(n.count(3)) ------> 2

print(n.count(4)) ------> 0

 

3. index() function: returns the index of first occurrence of the specified item.

Eg:

n=[1,2,2,2,2,3,3]

print(n.index(1)) ------>0

print(n.index(2)) ------>1

print(n.index(3)) ------>5

print(n.index(4)) ----->ValueError: 4 is not in list

 Note:

 If the specified element not present in the list then we will get

ValueError.

II. Manipulating elements of List:

1. append() function:

We can use append() function to add item at the end of the list.

Eg:

list=[]

list.append("A")

 list.append("B")

 list.append("C")

 print(list) ------------> ['A', 'B', 'C']

 

Eg:

To add all elements to list upto 100 which are divisible by 10

list=[]

for i in range(101):

if i%10==0:

list.append(i)

print(list) -------> [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

 

2. insert() function: To insert item at specified index position

n=[1,2,3,4,5]

n.insert(1,888)

print(n) -----> [1, 888, 2, 3, 4, 5]

Eg:

n=[1,2,3,4,5]

n.insert(10,777)

n.insert(-10,999)

print(n)-----------> [999, 1, 2, 3, 4, 5, 777]

 

Note:

If the specified index is greater than max index then element will be

inserted at last position.

If the specified index is smaller than min index then element will be

inserted at first position.

 append()    insert()
 It inserts the element at the last position i.e. end  of the list
 It inserts the element at the specified index position

3. extend() function:To add all items of one list to another list

list1.extend(list2)
all items present in list2 will be added to list1

4. remove() function: to remove specified item from the list.
If the item is present multiple times then only first occurrence will be removed.
And if the specified element is not present in the list, then there will be ValueError


5. pop() function: It removes and returns the last element of the list.


Note:

1. pop() is the only function which manipulates the list and returns some value

2. In general we can use append() and pop() functions to implement stack data structure by using list, which follows LIFO(Last In First Out) order.

In general we can use pop() function to remove last element of the list.

But we can use it to remove elements based on index.

list.pop(index)===>To remove and return element present at specified index.

list.pop()==>To remove and return last element of the list

Difference between remove() and pop():

                     remove()                    
                        pop()                             
 1. You need to specify the element to be removed                 
 1. You need to specify the index position from where the element is to be removed
 2. It does not return any value
 2. It returns the removed element
 3.If specified element is not available then we get ValueError.         
3. If list id empty, then we get IndexError.

 Note:
List objects are dynamic. i.e based on our requirement we can increase and decrease the size.
append(),insert() ,extend() ------>for increasing the size/growable nature remove(), pop() ------->for decreasing the size /shrinking nature


III. Ordering elements of List:

1. reverse():

We can use to reverse() order of elements of list.

n=[10,20,30,40]

n.reverse()

print(n) -------------->[40, 30, 20, 10]

 

2. sort() function:

In list by default insertion order is preserved. If want to sort the elements of list according to default natural sorting order then we should go for sort() method.

For numbers ==>default natural sorting order is Ascending Order

For Strings ==> default natural sorting order is Alphabetical Order

n=[20,5,15,10,0]

n.sort()

print(n) #[0,5,10,15,20]

 

s=["Dog","Banana","Cat","Apple"]

s.sort()

print(s) #['Apple','Banana','Cat','Dog']

 

Note: To use sort() function, compulsory list should contain only homogeneous elements. otherwise we will get TypeError

Eg:

  n=[20,10,"A","B"]

n.sort()

print(n)

 TypeError: '<' not supported between instances of 'str' and 'int'

 

Note: In Python 2 if List contains both numbers and Strings then sort() function first sort numbers followed by strings

n=[20,"B",10,"A"]

n.sort()

print(n)# [10,20,'A','B']

 But in Python 3 it is invalid.


To sort in reverse of default natural sorting order:


We can sort according to reverse of default natural sorting order by using reverse=True argument.
Example:

n=[40,10,30,20]

n.sort()

print(n) ------------->[10,20,30,40]

n.sort(reverse=True)

print(n) ------------->[40,30,20,10]

n.sort(reverse=False)

print(n) ---------->[10,20,30,40]

No comments:

Post a Comment