List Datatype and Methods in Python
In this class, we discuss list datatype and methods in python.
For Complete YouTube Video: Click Here
List Datatype in Python
The list is a collection/sequence of compound data types.
The list is ordered, changeable, and allows duplicates.
We understand the terms one by one below.
The list is defined using square brackets [].
Example: a=[1,”hello”,5.5]
The above example is a list containing three elements.
The three elements in the list are of different data types.
From the definition, its given list is a collection of compound data types. So different data types accepted in the above list example.
Ordered: List is ordered means whatever the order elements are defined in the list. The exact order elements are displayed.
Set and Dictionary data types do not maintain the order when we display.
Changeable: We can modify the list. We can add new elements. We can delete and change existing elements.
Duplicates: List allow duplicate value. Example a=[1,2,5,5]. Five is given two times.
Index: each element in the list is given an index value. Similar to strings.
The first element is given an index value of zero. The second element is given index value 1. and so on.
Negative indexes are also given. The last element is given -1, the second element from the last is given -2. So on.
We can access the elements in the list using these index values.
These indexes concept shown graphically in the strings class. Click here.
Examples:
a=[1,2,5,5]
print(a[2]) will display the element present in index position 2. In our example, element 5 is present.
print(a[-1]) negative indexes are assigned from the last to the first element. -1 displays the last element. i.e. 5.
Slicing
List slicing is applicable. I.e. we can access part of the list using slicing.
The concept of slicing explained in string slicing. Click here.
Example:
a=[“hello”,” this”,” is”,” python”,” program”,” list”,” example”]
print(a[2:5]) will display [“is”,” python”,” program”]
print(a[-5:-2]) will display [“is”,”python”,”program”]
Change Element:
Examples:
a=[“hello”,” this”,” is”,” python”,” program”,” list”,” example”]
a[3]=” java”
print(a) will display [“hello”,” this”,” is”,” Java”,” program”,” list”,” example”]
In this example element present in index position three is modified to java.
a[0:3]=[“hi”,” it”,” was”]
print(a) will display [“hi”,” it”,” was”,” Java”,” program”,” list”,” example”]. Multiple modifications.
Loop through the list.
We can loop through the list. For loop will take elements in the list one by one.
a=["hello","this","is","python","program","list","example"] for i in a: print(i) hello this is python program list example
in operator
We can use in operator to check element exists in the list or not.
a=["hello","this","is","python","program","list","example"] if "hello" in a: print("element exists") output: element exists
len Function
len function is used to check the length of the list. ie Number of elements present in the list.
a=["hello","this","is","python","program","list","example"] print(len(a)) Output: 7
List Methods in Python
Append method:
The list datatype and methods are mostly used when we write programming.
The append method is used to add elements at the end of the list.
Example:
a=[“hello”,”this”,”is”,”python”,”program”,”list”,”example”]
a.append(“monkey”)
print(a) will display [“hello”,”this”,”is”,”python”,”program”,”list”,”example”,”monkey”]
Insert Method:
insert method is used to insert the elements at the given position.
a=[“hello”,”this”,”is”,”python”,”program”,”list”,”example”]
a.insert(1,”example”)
The insert method will move the elements to the successive positions and place example in index position one.
print(a) will display [“hello”,”example”,”this”,”is”,”python”,”program”,”list”,”example”]
Remove Method:
remove method used to remove an element from the list.
Example:
a=[“hello”,”this”,”is”,”python”,”program”,”list”,”example”]
a.remove(“this”) remove method take element as input. It will identify the element and remove it from the list.
print(a) will display [“hello”,”is”,”python”,”program”,”list”,”example”]
Pop Method:
the pop method is used to delete an element from the list using an index value.
If the index value is not provided. default remove the last element.
a=[“hello”,”this”,”is”,”python”,”program”,”list”,”example”]
a.pop()
a.pop(1)
print(a) will display [“hello”,”is”,”python”,”program”,”list”]
Clear Method:
the clear method used to delete all the elements from the list.
a=[“hello”,”this”,”is”,”python”,”program”,”list”,”example”]
a.clear()
print(a) will display empty list.
Reverse Method:
The reverse method used to reverse the elements in the list.
a=[“hello”,”this”,”is”,”python”]
a.reverse()
print(a) will display [“python”,”is”,”this”,”hello”]
Sort Method:
Sort method used to sort the elements in the list.
a=[“hello”,”this”,”is”,”python”,”program”,”list”,”example”]
a.sort()
print(a) will display [“example”,”hello”,”is”,”list”,”program”,”python”,”this”].
Sort the elements in dictionary order.
Index Method:
index method returns the index of the first occurrence of the element.
a=[“hello”,”this”,”is”,”python”,”program”,”list”,”example”]
b=a.index(“is”)
print(b) will display 2. is present in index position 2.
More on methods we discuss whenever we solve placement examples.