In Python, we say many other compound data types that referred to a sequence. Python lists are one of the most used and versatile sequences. In Python List, we separate values by using comma enclosed in square brackets but values don’t need to belong to the same data type. Python Lists are just like dynamically sized arrays.
Creating List:
To create Python Lists we put values in a square bracket and separated by comma. For better understanding look the Example given below,
Example,
Accessing values in Lists:
To get to values in Lists, utilize the square brackets for cutting at the side of the index or files to get esteem accessible at that list.
list1 = ['ABC' , 'xyz' , 'MNO' ]; list2 = [1, 2, 3, 4, 5 ]; print ("list1[2]: ", list1[2]) print ("list2[1:4]: ", list2[1:4])
Updating Lists:
We can update elements of lists by giving the slice on the left-hand side of the assignment operator. To add to elements in a list the append() method used.
#updating list list = ['ABC' , 'xyz' , 'MNO']; print ("Value at index 2 : ") print (list[1]) list[2] = 'KLM' ; print ("New value at index 2 : ") print (list[2])
Deleting lists element:
Del statement is used to remove or delete any element from the list. Besides that, you can also use the remove() method if you know the element you want to delete.
list = ['ABC' , 'xyz' , 'MNO']; print list del list[1]; print ("After deleting value at index 2 : ") print list
Basic Lists Operations:
Some basic Operations used in lists are given below,
Expression | Description | Output |
len ([a, b, c]) | Length | 3 |
[a, b, c]+[x, y, z] | Concatenation | [a, b, c, x, y, z] |
u in [u, v, w ] | Membership | True |
[KARL]*3 | Repetition | KARL,KARL.KARL |
for y in [l, m, n]: print y | Iteration | l, m, n |
Built-In Lists Functions:
There are some Built-in functions we found in list, Here is a table for those functions,
Functions | Description |
cmp(list1, list2) | This function is used to Compares elements of both lists. |
max(list) | This function is used to Returns items from the list with max value. |
min(list) | This function is used to Returns items from the list with min value. |
list(seq) | This function is used to Converts a tuple into the list. |
len(list) | This function is used to Gives the total length of the list. |
Built-In Lists Methods:
There are also some Built-in Methods we found in list, Here is a table for those functions,
Methods | Description |
list.append(obj) | This method is used to Appends object obj to list. |
list.index(obj) | This method is used to Returns the lowest index in the list that obj appears. |
list.extend(seq) | This method is used to Appends the contents of seq to list. |
list.count(obj) | This method is used to Returns count of how many times obj occurs in the list. |
list.reverse() | This method is used to Reverses objects of the list in place. |
list.sort([func]) | This method is used to Sorts objects of the list, use compares function if given. |
list.remove(obj) | This method is used to Removes object obj from the list. |
list.pop(obj=list[-1]) | This method is used to Removes and returns the last object or obj from the list. |
list.insert(index, obj) | This method is used to Inserts object obj into the list at offset index. |