In Python, a list is one of the data types hold the sequence of data. In other words a list is a collection of different values or items. In Python the lists are mutable, means we can change the elements of the list after forming it. The comma(,) is used as a separator of each item or element and square bracket [] is used to hold all the elements of one list separated by comma.
Although six Python data types can hold the sequence of items, but the list is most common and reliable among all. Lists written in Python are identical to array list in Java or vector in c++.
List declaration codes:
#a simple list
list1 = [1, 2, "Python", "Program", 98.6] list2 = ["mango", "guava", "papaya", "litchi"]
#printing the list print(list1) print(list2)
#printing the type of list |
---|
print(type(list1))
|
---|
Output:
[1, 2, 'Python', 'Program', 98.6] ['mango', 'guava', 'papaya', 'litchi'] <class 'list'> <class 'list'>
|
---|
Characteristics of Lists:
The characteristics of the list are as follows:
- The lists are in order.
- The list element can be accessed via the index.
- The list is a mutable type.
- The number of various elements can be stored in a list.
- The rundowns are changeable sorts.
Ordered List Checking :
#example1
list1 = [1, 2, "Python", "Program", 98.6] list2 = [1, 2, 98.6, "Python", "Program"]
|
---|
list1==list2 #example 2
list3 = [1, 2, "Python", "Program", 98.6] list4 = [1, 2, "Python", "Program", 98.6]
list3==list4 |
---|
|
---|
Output:
In above examples, the elements of both the lists are same but order is different in example 1, so it is showing 'False'. But when list elements and order both are same in example 2, it is showing 'True'.
Records forever protect the component's structure. Because of this, it is an arranged collection of things, Let's take a closer look at the list example:
#list example in detail
emp = ["Tim", 102, "USA"] Dep1=["Acc", 10] Dep2=["IT", 11] |
---|
HOD_Acc=[10, "Mr. Wilson"] HOD_IT=[11, "Mr. Samson"] print("printing employee data......")
print("Name:%s, ID:%d, Country:%s"%(emp[0], emp[1], emp[2])) print("printing departments.....") print("Department 1:\n, Name: %s, ID: %d\n Department 2:\n, Name: %s, ID: %d"%(Dep1[0], Dep1[1], Dep2[0], Dep2[1])) print("HOD details.....") print("Acc HOD Name: %s, ID: %d"%(HOD_Acc[1], HOD_Acc[0])) print("IT HOD Name: %s, ID: %d"%(HOD_IT[1], HOD_IT[0])) print(type(emp), type(Dep1), type(Dep2), type(HOD_Acc), type(HOD_IT)) |
---|
|
---|
Output:
printing employee data...... Name: Tim, ID: 102, Country: USA printing departments..... Department 1: Name: Acc, ID: 10
Department 2: Name: IT, ID: 11 |
---|
printing HOD details..... Acc HOD Name: Mr. Wilson, ID: 10
IT HOD Name: Mr. Samson, ID: 11 |
---|
<class 'list'> <class 'list'> <class 'list'> <class 'list'> <class 'list'>
|
---|
Here we have printed the employee and department specific details from list that we had created.
List Indexing and Splitting:
The indexing procedure in list is similar to that as in string processing. The slice operator [] can be used to hold the list components.
The index ranges from 0. The 0th index is where the index first element is stored, the 1st element is where the index second element is stored and so on..
List = [0, 1, 2, 3, 4, 5]
List[0]=0 List[0:]=[0, 1, 2, 3, 4, 5]
List[1]=1 List[:]=[0, 1, 2, 3, 4, 5]
List[2]=2 List[2:4]=[2, 3]
List[3]=3 List[1:3]=[1, 2]
List[4]=4 List[:4]=[0, 1, 2, 3]
List[5]=5
We can get the sub list of the list using the following syntax .
list_variable=(start:stop:step) |
---|
- Here, the start indicates the beginning record position of the rundown.
- The stop indicates the last record position of the rundown.
- Within a start, the step is used to skip the nth element: stop.
The start parameter is the initial index, the step is the ending index, and the value of the end parameter is the number of elements that are "stepped" through. The default value for the step is one without a specific value. Inside the resultant Sub List, the same with record start would ber available, yet the one with the file finish will not, The first element in a list appears to have an index of zero.
Consider the following example:
list=[1, 2, 3, 4, 5, 6, 7]
print(list[0]) print(list[1]) print(list[2])
|
---|
print(list[3]) print(list[:]) print(list[0:6])
print(list[2:5]) print(list[1:6:2]) |
---|
|
---|
Output:
1 2 3 4 [1, 2, 3, 4, 5, 6, 7]
|
---|
In opposite to other programming language, Python lets you use negative indexing as well. The negative indices are counted from right. Negative indexing starts from -1 and it indicated the rightmost element of the list, -2 indicates the second last, -3 for third last and this pattern continues untill the last element in the leftmost side arrives.
(forward direction)
0 1 2 3 4 5
-6 -5 -4 -3 -2 -1
(backward direction)
Lets have a look at the followimg example, here we'll use negative indexing to the elements of the list.
Negative indexing example:
list=[1, 2, 3, 4, 5]
print(list[-1]) print(list[-3:]) print(list[:-1])
|
---|
print(list[-3:-1])
|
---|
Output:
Updating List Values:
Due to their mutability and the slice and assignment operator's ability to update their values, lists are Python's most adaptable data structure. Python's append() and insert() methods can also add values to a list.
Consider the following example to upodate the values inside the list.
Code:
list=[1, 2, 3, 4, 5]
print(list) #now assign new value to the second index list[2]=10 print(list) #adding multiple element list[1:3]=[89,78] print(list) #adding value at the end of the list list[-1]=25
|
---|
print(list)
|
---|
Output:
[1, 2, 3, 4, 5] [1, 2, 10, 4, 5]
|
---|
Python List Operations
The concatenation (+) and the repetition (*) operators work in the same way as they were working with the strings. The different operators of list are:
- Repetition
- Concatenation
- Length
- Iteration
- Membership
Let's see how the list responds to various operators.
1. Repetition
The redundancy administrator empowers the rundown components to be rehashed on different occasions.
Code:
#declaring the list list=[1, 2, 3, 4, 5]
print(list) #repetition of list r = list*2 print(r)
|
---|
|
---|
Output:
[1, 2, 3, 4, 5] [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
|
---|
2. Concatenation:It concatenates the list mentioned mentioned on either side of the operator.
Code:
#declaring the lists list1=[1, 2, 3, 4, 5] list2=[6, 7, 8, 9, 10]
#concatenating the lists print(list1+list2)
|
---|
|
---|
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
---|
3. Length:It is used to get the length of the list.
Code:
#declaring the list list1=[10, 12, 14, 16, 18, 20, 22, 24]
#size of the lists len(list1)
|
---|
|
---|
Output:
4. Iteration:
The for loop is used to iterate over the list elements.
Code:
#declaring the list list1=[10, 12, 14, 16, 18, 20, 22, 24]
#iterating for i in list1: print (i) |
---|
|
---|
5. Membership:
It returns true if a particular item presents in a particular list otherwise false.
Code:
#declaring the list list1=[10, 12, 14, 16, 18, 20, 22, 24]
#membership of the list #returns true if value exists in list otherwise false. print(14 in list1) print(15 in list1) print(100 in list1) print(22 in list1)
|
---|
|
---|
Adding Elements to the List:
The append() function in Python can add a new item to the list. In any case, the annex() capability can enhance the finish of the rundown.
Consider the following model, here we take the components of the rundown from the client and print the rundown on the control center.
Code:
#declaring the empty list list=[]
#number of elements will be entered by the user n=int(input("Enter the number of elements in the list:")) #for loop to take the input for i in range(0, n): #the input is taken from the user and added to the list as the items list.append(input("Enter the item:")) print("printing the list items") #traversal loop to print the list items for i in list: print(i, end=" ")
|
---|
|
---|
Output:
Enter the number of elements in the list:10 Enter the item: 32 Enter the item: 88 Enter the item: 46 Enter the item: 17 Enter the item: 42 Enter the item: 29 Enter the item: 58 Enter the item: 61 Enter the item: 45 Enter the item: 73 printing the list items 32 88 46 17 42 29 58 61 45 73
|
---|
Removing Elements from the List
The remove() function in Python can remove an element from the list. Let's implement the remove() function in Python code and check the output.
Code:
list=[0, 1, 2, 3, 4]
print("printing original list:") for i in list: print(i,end"=") list.remove(2) print("/nprinting the list after removal of first element...") for i in list: print(i,end"=") print("printing the list items") #traversal loop to print the list items for i in list: print(i, end=" ")
|
---|
|
---|
Output:
printing original list: 0, 1, 2, 3, 4 printing the list after removal of first element... 0, 1, 3, 4
|
---|
Python List Built-In-Functions:
Python provides the following built in functions which can be used with the list.
len(): It is used to calculate the length of the list.
Code:
Output:
max(): It returns the maximum element of the list.
Code:
Output:
min(): It returns the minimum element of the list.
Question: Write a Python program to find out a common element between two lists.
Code:
list1=[0, 1, 2, 3, 4] list2=[8, 7, 6, 5, 4]
for x in list1: for y in list2: if x==y: print("The common element is",x)
|
---|
|
---|
No comments:
Post a Comment