Python List is a composite data type in which we store many objects and it is the most versatile and most widely used data type of Python. We can write comma separated items in the list and put all the items between square brackets.
List can store different types of items, it is not necessary to have same type of items. List can store the duplicate items, it is changeable and ordered in nature.
Click here to know the basics of Python
Table of Contents
Syntax of List
The syntax of python list is simple. Just we have to put the item inside the square brackets.
geekList = 
squares = [1, 4, 9, 16, 25] print(squares)
Output: [1, 4, 9, 16, 25]
Accessing item from List
We can access item from the list by using index.  is the index of the first item,  is the index of the second item.
squares = [1, 4, 9, 16, 25] print(squares) print(squares[-5]) print(squares)
Output: 1 1 16
Slicing of the List
Slicing operations return a new list from the existing list. In other words, we can say that we find the subset of the list by using slicing. We use :(colon) to shorten the list.
squares = [1, 4, 9, 16, 25] print(squares[:]) print(squares[2:]) print(squares[:3])
Output: [1, 4, 9, 16, 25] [9, 16, 25] [1, 4, 9]
Concatenation of Python List
This is the way to append one list to another list. Suppose we have two list L1 and L2 then we will find new list by L3 = L1 + L2.
squares = [1, 4, 9, 16, 25] squares = squares + [36, 49, 64, 81, 100] print(squares)
Output: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Update List item
We can update the list item of the given index.
squares = [1, 4, 9, 16, 25] squares = 100 print(squares)
Output: [1, 4, 9, 100, 25]
Append List item
We use append() method to append item in the list.
squares = [1, 4, 9, 16, 25] squares.append(36) print(squares)
Output: [1, 4, 9, 16, 25, 36]
Assignment to Python List Slices
Here we assign a second list to the list between the given slices.
list = [1, 4, 9, 16, 25] list[2:3] = ['C', 'D', 'E'] print(list)
Output: [1, 4, 'C', 'D', 'E', 16, 25]
We use len() method to find the length of the List
list = [1, 4, 9, 16, 25] print(len(list))
Remove item from List
We use remove() method to remove item from the List.
list = ["apple", "banana", "mango"] list.remove("apple"); print(list)
Output: ['banana', 'mango']
Functions of Python List
|1||min(list)||The min method returns the minimum value from the list.|
|2||max(list)||The max method returns the maximum value from the list.|
|3||len(list)||Returns the total number of items from the list.|
|4||cmp(list1, list2)||This method compares two list items.|
|5||list(sequence)||Converts another sequence to list.|
|6||insert(index, value)||Insert element at given position.|
|7||append(list)||Adds another list at last position.|
|8||extend(seq)||Appends the contents of seq to the list.|
|9||remove(item)||Removes element from list.|
|10||clear()||This method clears list.|
|11||index(item)||Finds the index of given item.|
|12||count(item)||Counts how many times items are available in the list.|
|13||sort()||Sorts element in ascending order.|
|14||reverse()||We reverses items of the list in place.|
|15||copy()||Makes the copy of the list.|