In python, a list can be defined as a collection of values or items of different types. The items in the list are separated with the comma (,) and enclosed with the square brackets [].
List = [value1, value2, value3,….]
L0 = [] #creates empty list
L1 = [123,"python", 3.7]
L2 = [1, 2, 3, 4, 5, 6]
L3 = ["C Programing","Java","Python"]
print(L0)
print(L1)
print(L2)
print(L3)
[]
[123, 'python', 3.7]
[1, 2, 3, 4, 5, 6].
['C Programing','Java','Python']
The indexing are processed in the same way as it happens with the strings. The elements of the list can be accessed by using the slice operator [].
The index starts from 0, the first element of the list is stored at the 0th index, the second element of the list is stored at the 1st index, and so on.
Python provides us the flexibility to use the negative indexing also. The negative indices are counted from the right. The last element (right most) of the list has the index -1, its adjacent left element is present at the index -2 and so on until the left most element is encountered.
Operator | Description |
---|---|
+ |
It is known as concatenation operator used to concatenate two lists |
* |
It is known as repetition operator. It concatenates the multiple copies of the same list. |
[ ] |
It is known as slice operator. It is used to access the list item from list. |
[ : ] |
It is known as range slice operator. It is used to access the range of list items from list. |
in |
It is known as membership operator. It returns if a particular item is present in the specified list. |
not in |
It is also a membership operator and It returns true if a particular list item is not present in the list. |
num=[1,2,3,4,5]
lang=['python','c','java','php']
print(num+lang)
print(num*2)
print(lang[2])
print(lang[1:4])
print('cpp' in lang)
print(6 not in num)
[1, 2, 3, 4, 5, 'python', 'c', 'java', 'php']
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
java
['c', 'java', 'php']
False
True
Python allows us to modify the list items by using the slice and assignment operator.
We can use assignment operator (=) to change an item or a range of items.
num=[1,2,3,4,5]
print(num)
num[2]=30
print(num)
num[1:3]=[25,36]
print(num)
num[4]="Python"
print(num)
[1, 2, 3, 4, 5]
[1, 2, 30, 4, 5]
[1, 25, 36, 4, 5]
[1, 25, 36, 4, 'Python']
Python allows us to delete one or more items in a list by using the del keyword.
num=[1,2,3,4,5]
print(num)
del num[1]
print(num)
del num[1:3]
print(num)
[1, 2, 3, 4, 5]
[1, 3, 4, 5]
[1, 5]
Python provides the following built-in functions which can be used with the lists.
In Python len() is used to find the length of list,i.e it returns the number of items in the list.
Syntax:len(list)
num=[1,2,3,4,5,6]
print(“length of list :”,len(num))
length of list : 6
In Python max() is used to find maximum value in the list
Syntax:max(list)
num=[1,2,3,4,5,6]
lang=['java','c','python','cpp']
print("Max of list :",max(num))
print("Max of list :",max(lang))
Max of list : 6
Max of list : python
In Python min() is used to find minimum value in the list
Syntax:min(list)
num=[1,2,3,4,5,6]
lang=['java','c','python','cpp']
print("Min of list :",min(num))
print("Min of list :",min(lang))
Min of list : 1
Min of list : c
In python, sum(list) function returns sum of all values in the list. List values must in number type.
Syntax:sum(list)
num=[1,2,3,4,5,6]
print(“sum of list items :”,sum(num))
sum of list items: 21
In python, sorted(list) function is used to sort all items of list in an ascending order.
Syntax:sorted(list)
num=[1,3,2,4,6,5]
lang=['java','c','python','cpp']
print(sorted(num))
print(sorted(lang))
[1, 2, 3, 4, 5, 6]
['c', 'cpp', 'java', 'python']
The list() method takes sequence types and converts them to lists. This is used to convert a given string or tuple into list.
Syntax:list(sequence)
str="python"
list1=list(str)
print(list1)
['p', 'y', 't', 'h', 'o', 'n']
In python append() method adds an item to the end of the list.
Syntax:list.append(item)
-item may be number,string,list and etc
num=[1,2,3,4,5]
lang=['python','c','java','php']
num.append(6)
print(num)
lang.append("cpp")
print(lang)
[1, 2, 3, 4, 5, 6]
['python', 'c', 'java', 'php', 'cpp']
In python remove() method removes the first item from the list which is equal to the passed value. It throws an error if the item is not present in the list.
Syntax:list.remove(item)
-item may be number,string,list and etc
num=[1,2,3,4,5]
lang=['python','c','java','php','c']
num.remove(2)
print(num)
lang.remove("c") # first occurence will remove
print(lang)
lang.remove("cpp")
print(lang)
[1, 3, 4, 5]
['python', 'java', 'php', 'c']
ValueError:list.remove(x):x not in list
In python sort() method sorts the list elements. It also sorts the items into descending and ascending order. It takes an optional parameter 'reverse' which sorts the list into descending order. By default, list sorts the elements into ascending order.
Syntax:list.sort ([reverse=true])
-reverse, which displays items in descending
lang = ['p', 'y', 't', 'h', 'o','n'] # Char list
even = [6,8,2,4,10] # int list
print(lang)
print(even)
lang.sort()
even.sort()
print("\nAfter Sorting:\n")
print(lang)
print(even)
print("In Descending Order :\n")
even.sort(reverse=True)
print(even)
['p', 'y', 't', 'h', 'o', 'n']
[6, 8, 2, 4, 10]
After Sorting:
['h', 'n', 'o', 'p', 't', 'y']
[2, 4, 6, 8, 10]
In Descending Order :
[10, 8, 6, 4, 2]
In python reverse() method reverses elements of the list. If the list is empty, it simply returns an empty list. After reversing the last index value of the list will be present at 0 index.
Syntax:list. reverse ()
lang = ['p', 'y', 't', 'h', 'o','n']
lang = ['p', 'y', 't', 'h', 'o','n']
print("After reverse")
lang.reverse()
print(lang)
After reverse
['n', 'o', 'h', 't', 'y', 'p']
In python count() method returns the number of times element appears in the list. If the element is not present in the list, it returns 0.
Syntax:list.count(item)
num=[1,2,3,4,3,2,2,1,3,4,5,7,8]
cnt=num.count(2)
print("Count of 2 is:",cnt)
cnt=num.count(10)
print("Count of 10 is:",cnt)
Count of 2 is: 3
Count of 10 is : 0
In python index () method returns index of the passed element. This method takes an argument and returns index of it. If the element is not present, it raises a ValueError.
If list contains duplicate elements, it returns index of first occurred element.
This method takes two more optional parameters start and end which are used to search index within a limit.
list.index(x[, start[, end]])
lang = ['p', 'y', 't', 'h', 'o', 'n', 'p','r','o','g','r','a','m']
print("index of t is:",lang.index('t'))
print("index of p is:",lang.index('p'))
print("index of p is:",lang.index('p',3,10))
print("index of p is:",lang.index('z'))
index of t is: 2
index of p is: 0
index of p is: 6
ValueError: 'z' is not in list
In python insert() method inserts the element at the specified index in the list. The first argument is the index of the element before which to insert the element.
Syntax:list.insert(i,x)
i : index at which element would be inserted.
x : element to be inserted.
num=[10,20,30,40,50]
num.insert(4,60)
print("updated list is :",num)
num.insert(7,70)
print("updated list is :",num)
updated list is : [10, 20, 30, 40, 60, 50]
updated list is : [10, 20, 30, 40, 60, 50, 70]
In python pop() element removes an element present at specified index from the list. It returns the popped element.
Syntax:list.pop([i])
num=[10,20,30,40,50]
num.pop()
print("updated list is :",num)
num.pop(2)
print("updated list is :",num)
num.pop(7)
print("updated list is :",num)
updated list is : [10, 20, 30, 40]
updated list is : [10, 20, 40]
IndexError: pop index out of range
In python clear() method removes all the elements from the list. It clears the list completely and returns nothing.
Syntax:list.clear()
num=[10,20,30,40,50]
num.clear()
print("After clearing ",num)
After clearing [ ]