List concepts in python part 2

By
There are some basic list operations in python as given below.
  • Lists are mutable but strings are immutable.
e.g.
animals=['cow','horse','lion','tiger','deer']
numbers=[1,2,3,4,5,6,7,8,9,10]
#Replace list items
animals[4]='dog'
numbers[2:5]=[30,40,50]
print(animals)
print(numbers)
#delete items from 7 to 9 in numbers list
numbers[6:9]=[]
print(numbers)
#clear all items in the numbers list
numbers[:]=[]
print(numbers)
Output:-
['cow', 'horse', 'lion', 'tiger', 'dog']
[1, 2, 30, 40, 50, 6, 7, 8, 9, 10]
[1, 2, 30, 40, 50, 6, 10]
[]
>>> 
  • The Repetition operator is used to repeat  the elements in the list multiple times as given below.
e.g.
list1=[10,20,30,40,50]
list2=list1*2
print(list2)
Output:-
[10, 20, 30, 40, 50, 10, 20, 30, 40, 50]
>>> 
  • The concatenation operator is used to concatenate the one list with other list.
e.g.
list1=[10,20,30,40,50]
list2=['a','b','c','d','e','f']
list3=list1+list2
print(list3)
Output:-
[10, 20, 30, 40, 50, 'a', 'b', 'c', 'd', 'e', 'f']
>>> 
  • The membership operator returns True if a particular element exists in a list otherwise False.
e.g.
list=['a','e','i','o','u']
print('a' in list)
print('o' in list)
print('p' in list)
Output
True
True
False
  • A list can be iterated by using for loop as given below.
e.g.
list=['Ram',20,3.5,'lion','neha']
for i in list:
    print(i)
    #print(i,end=" ")
Output:
Ram
20
3.5
lion
neha
>>> 

There are some basic operations can be performed on a list.
  • Create a list
e.g.
list=['Ram',20,3.5,'lion','neha']
  • Concatenation (addition) of two more lists.
e.g.
list1=[10,20,30,40,50]
list2=[60,70,80,90,100]
list=list1+list2
print(list)
Output:
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
>>> 
  • del() function is used to delete the elements in the list.
e.g.
list1=[10,20,30,40,50,60,70,80,90,100]
#delete the 3rd index value from the list
del(list1[3])
print(list1)
#delete the 6th index to last index from the list
del(list1[6:])
print(list1)
#delete the whole list
del(list1[:])
print(list1)
Output:
[10, 20, 30, 50, 60, 70, 80, 90, 100]
[10, 20, 30, 50, 60, 70]
[]
>>> 
  • len() function is used to calculate the number of items in given list.
e.g.
list1=[10,20,30,40,50,60,70,80,90]
a=len(list1)
print("Lenght of given list is = ",a)
Output:
Lenght of given list is =  9
>>> 
  • list() function is used to convert the string to a list.
e.g.

str="AMERICA"
a=list(str)
print("Convert the string value to the list value: ",a)
Output:
Convert the string value to the list value:  ['A', 'M', 'E', 'R', 'I', 'C', 'A']
>>> 
  • max() function is used in python to return the maximum element in the list.
e.g.
str1=['A','M','E','R','I','C','A']
str2=[1,2,3,4,5,6,7,8]
print("Maximum value of list is=",max(str1))
print("Maximum value of list is=",max(str2))
Output:-
Maximum value of list is= R
Maximum value of list is= 8
>>> 
  • min() function is used in python to return the minimum element in the list.
e.g.
str1=['A','M','E','R','I','C','A']
str2=[1,2,3,4,5,6,7,8]
print("Minimum value of list is=",min(str1))
print("Minimum value of list is=",min(str2))
Output:-
Minimum value of list is= A
Minimum value of list is= 1
>>> 
  • sorted() function is used to return the sorted list without any change in the list.
e.g.
str1=['A','M','E','R','I','C','A']
str2=[70,50,60,40,20,80,30,40]
print("Sorted value of the list is=",sorted(str1))
print("Sorted value of the list is=",sorted(str2))
Output:-
Sorted value of the list is= ['A', 'A', 'C', 'E', 'I', 'M', 'R']
Sorted value of the list is= [20, 30, 40, 40, 50, 60, 70, 80]
>>> 
  • sum() function is used to return the sum of all the list values.
e.g.
str1=[70,50,60,40,20,80,30,40]
print("sum of all list value is=",sum(str1))
Output:-
sum of all list value is= 390
>>> 
  • Comparison between two list is possible.Comparison is done item by item till there is mismatch.
e.g.
a=[1,5,10,15,20]
b=[1,5,10,30,3]
if(a<b):
    print("a is less than b")
elif(a==b):
    print("a is equal to b") 
else:
     print("b is less than a")
Output:-
a is less than b
>>> 

List Methods:- 
List methods are accessed using the syntax list.function().
There are important methods used in list operations as given below:-
  • list.append():-This is used to add the new items at the end of the list.This Method takes only one argument.
e.g.
lst=[10,20,30,40,50,60,70]
print(lst)
lst.append(90)
print(lst)
Output:-
[10, 20, 30, 40, 50, 60, 70]
[10, 20, 30, 40, 50, 60, 70, 90]
>>> 
  • list.remove():-This method is used to delete the item from the list.
e.g.
lst=[10,20,30,40,50,60,70]
print(lst)
lst.remove(40)
print(lst)
Output:-
[10, 20, 30, 40, 50, 60, 70]
[10, 20, 30, 50, 60, 70]
>>> 
  • list.pop():-This method is used to remove the last item from the list.
e.g.
list=[10,20,30,40,50,60,70]
print(list)
list.pop()
print(list)
#delete the 4th index value in the list
list.pop(4)     
print(list)
Output:-
[10, 20, 30, 40, 50, 60, 70]
[10, 20, 30, 40, 50, 60]
[10, 20, 30, 40, 60]
>>> 
  • list.insert():-This method is used to insert the element at the specified position in the list.This method takes two argument.first argument denote the index value and second argument denote item which you want to add in the given list.
e.g.
list=[10,20,30,40,50,60,70]
print(list)
#insert the item at the 4th index in the list
list.insert(4,122)
print(list)
Output:-
[10, 20, 30, 40, 50, 60, 70]
[10, 20, 30, 40, 122, 50, 60, 70]
>>> 
  • list.reverse():-This is used to reverse the element in the list.
e.g.
list=[2,4,6,8,10,12,14,16]
list.reverse()
print(list)
Output:-
[16, 14, 12, 10, 8, 6, 4, 2]
>>> 
  • list.sort():-This is used to sort the items in the list.
e.g.
list=[70,20.5,40,50,60,16]
list.sort()
print(list)
Output:-
[16, 20.5, 40, 50, 60, 70]
>>>  
  • list.count():-This is used to return the number of times element appears in the list.
e.g.
list=[16,40,40,50,60,70,40,20,40]
a=list.count(40)
print("Above given number present in the list: ",a)
Output:-
Above given number present in the list:  4
>>> 
  • list.index():-This used to find the index of the particular item in the list.
e.g.
list=[16,40,50,60,70,20,40]
a=list.index(70)
print("70 is present in the list at index : ",a)
Output:-
70 is present in the list at index :  4
>>> 
List Varieties:-
There are some list varieties as given below:-
  • It is possible to create new list from another.
e.g.
list1=[16,40,50,60,70,20,40]
list2=list1
print(list1)
print(list2)
Output:-
[16, 40, 50, 60, 70, 20, 40]
[16, 40, 50, 60, 70, 20, 40]
>>> 
Note:-list1 and list2 are pointing the same list.
  • It is possible to create a list of lists.
e.g.
a=[1,3,5,7,9,11,13]
b=[2,4,6,8,10,12,14]
c=[a,b]
print(c[0][4],c[1][6])
#c[0][4]-> 0th list(first list) of 4th index
#c[1][6]-> 1th list(second list) of 6th index
Output:-
9 14
>>>
  • It is possible that a list may be embedded in another list.
e.g.
a=[1,3,5,7,9,11,13]
b=[2,a,4,6,8,10,12,14]
print("After embedded list is: ",b)
Output:-
After embedded list is:  [2, [1, 3, 5, 7, 9, 11, 13], 4, 6, 8, 10, 12, 14]
>>> 
  • It is possible to unpack a list within a list using the * operator.
e.g
a=[1,3,5,7,9,11,13]
b=[2,4,6,*a,8,10,12]
print("After unpacked list is: ",b)
Output:-
After unpacked list is:  [2, 4, 6, 1, 3, 5, 7, 9, 11, 13, 8, 10, 12]
>>> 
Program:1 
How to add items in empty list by user manually.
#declare the empty list
list=[]
num=int(input("Enter the number of elements in the list : "))
for i in range(0,num):
    #The input is taken by user and add to the list
    list.append(input("Enter the value:"))
#print the list
for j in list:
    print(j,end="")
Output:-
Enter the number of elements in the list : 10
Enter the value:1
Enter the value:2
Enter the value:3
Enter the value:4
Enter the value:5
Enter the value:6
Enter the value:7
Enter the value:8
Enter the value:9
Enter the value:10
12345678910
>>> 
Program:2
Write a program to remove the duplicate items of the list.
l1=[10,20,30,40,50,20,20,70,10,20,30]
#declare the empty list
l2=[]
for i in l1:
    if i not in l2:
        l2.append(i)

print("After remove duplicate item, list is= ",l2)
Output:-
After remove duplicate item, list is=  [10, 20, 30, 40, 50, 70]
>>> 
Program:3
Write a program to find the common element between two list.
l1=[1,3,5,7,9,11]
l2=[2,4,5,8,10,12,9]
for i in l1:
    for j in l2:
        if(i==j):
            print("The common element is: ",i)
Output:-
The common element is:  5
The common element is:  9
>>> 
For More..
Watch complete Lecture 25 Video:-


0 comments:

Post a Comment

Powered by Blogger.