List
List in Python:
List is a process by which we can store multiple data in sequential order. It is store different type of values which are separated with comma and all items are enclosed with square brackets ([ ]). The elements in List are mutable (i.e. changeable).
The list can be as follows:
The list can be as follows:
list1=[1,2,3,4,5,6,7,8,9] |
emp=[101,'Manager',40000] |
Indexing process in List:
In list the indexing is a process for storing data into it. When we store data into list, the data stored as index wise automatically which is start from '0' and goes to length -1. The first element of list stored into 0th Index and the 2nd element is stored into 1st Index and so on.
Consider that we want to store 5 marks of a student into list as follows:
stu=[60,89,78,88,92]
The indexing process given below:
Python has provide the negative indexing unlike other programming languages. The negative index start from right side and the right most element of the list is -1 and then -2 and so on.
odd=[]
for x in range(1,11):
if(x%2==1):
s=x**2
odd.append(s)
print(odd)
>>>
evn=[]
od=[]
for x in range(1,11):
if(x%2==0):
evn.append(x)
else:
od.append(x)
print("Even",evn)
print("Odd",od)
Consider that we want to store 5 marks of a student into list as follows:
stu=[60,89,78,88,92]
The indexing process given below:
Figure 1.1 |
Example 1:
Here we make a list which store the square of all odd numbers between 1 to 10.odd=[]
for x in range(1,11):
if(x%2==1):
s=x**2
odd.append(s)
print(odd)
Output:
[1, 9, 25, 49, 81]>>>
Example 2:
Here we make two list for odd and even where we store odd and even numbers from the range of 1 to 10.evn=[]
od=[]
for x in range(1,11):
if(x%2==0):
evn.append(x)
else:
od.append(x)
print("Even",evn)
print("Odd",od)
Output:
Even [2, 4, 6, 8, 10]
Odd [1, 3, 5, 7, 9]
>>>
Update and delete items in List:
Already we know that the values of list in python are immutable, means we are unable to change the value of list, but we can update the value of list by using the slice operator ( [] ) and assignment operator.Python also provide the method append () to add values into list.
Example 1:
List = [1, 2, 3, 4, 5, 6]print(List) #print all elements in list
print(List[2:]) #print all elements from 2nd position
print(List[:4]) #print all elements till 4th position
print(List[1:3]) #print all elements from 1st to 3rd position but it exclude the element in 3rd index
List[2]=10; # update value in 2nd index
print(List)
List[1:3]=[89, 78] #update values from 1st to 3rd index
print(List)
Output:
[1, 2, 3, 4, 5, 6][3, 4, 5, 6]
[1, 2, 3, 4]
[2, 3]
[1, 2, 10, 4, 5, 6]
[1, 89, 78, 4, 5, 6]
>>>
In list we can also delete the elements by using del keyword.
Example 2:
List = ["Apple","Mango","Banana","Guava","Grapes"]print(List)
del List[0]
print(List)
del List[1:3]
print(List)
del List[1]
print(List)
Output:
['Apple', 'Mango', 'Banana', 'Guava', 'Grapes']['Mango', 'Banana', 'Guava', 'Grapes']
['Mango', 'Grapes']
['Mango']
>>>
Example 3:
Linear Search technique in List. Here the append () method used to append the items into the list.list=[]
flag=False
n=int(input("Enter the limit"))
for i in range(0,n):
num=int(input("Enter the number to be stored into list"))
list.append(num)
sh=int(input("Enter number to be searched"))
for i in range(len(list)):
if(sh==list[i]):
flag=True
a=i+1
break
if(flag==True):
print("Element found position is:",a)
else:
print("Element not found")
Output:
Enter the limit10
Enter the number to be stored into list11
Enter the number to be stored into list22
Enter the number to be stored into list33
Enter the number to be stored into list44
Enter the number to be stored into list55
Enter the number to be stored into list66
Enter the number to be stored into list77
Enter the number to be stored into list88
Enter the number to be stored into list99
Enter the number to be stored into list101
Enter number to be searched55
Element found position is: 5
>>>
Comments
Post a Comment
If you have any doubt, Please let me know..