Python Lists
Python lists are the most commonly used collection that can also store duplicate and heterogeneous data items. These list are in sequence or order that is why these are also called ordered list. All new items or data values will be added in the last of the list (append).
List Syntax:
Python list is simple and flexible, which is much similar to php and javascript programming languages.
list_identifier = [ item1, item2, item3, ..........itemN ]
Where list identifier is the valid identifier/list name. This identifier will be used to access the elements of the list. List may contain no of data items as per requirement. These items may or may not be of similar types. In most common case these are of same type. We can also create blank list identifier such as list = [ ] and later assign values to this one.
Python list example:
print(fruits)
The above example will print all elements from the list in the same sequence as these are specified/stored.
Output:
['Banana', 'Gava', 'Graps', 'Cherry']
Access list elements:
We can easily access all or individual elements from the list using their index value. Index value of first item in python list is 0. The index of last data item will be N - 1 , Where N is the total no of list elements. If we want to print second element from the fruit list then we will write print ( fruit[ 1 ] ). This will print Gava.
If we provide wrong index for accessing list element then python will raise list index out of range.
print(fruits[2])
#This will print Gava
Python list append:
In python list we can easily append new item after the last index of list. This item also may of any type. Python automatically assign it new index next to the last index. For example if list already has last index 4 then new item will have index 5 after append operation.
Python list length
We can get the size of list using python built-in function. This method will return the total no of elements in a list. This is an integer number that is equla to the last index +1 of list. Python len function is used to determine list length.
List Length Function Syntax:
len ( list )
Python list pop method:
Another important function relevant to the list implementation is pop() function. This function track the total elements from the list. When thismethod is called it fetch the last inserted element from the list. In single execution it hold the current fetched index location in list implementation. When we again call pop function (second time), then it will return the second last element from the list. This is also used for remove first element from list.
Python pop syntax:
variable = list.pop( )
We can use pop function multiple time. If we call pop method more than the items in list the IndexError: pop from empty list error will occur.
Convert list to string python:
Join method is used to convert list elements to string. We can call join method from a string (used as separator) to join elements of list. Here we are joining array elements using space. We can also provide any other valid character as array elements separator.
Example:
fruits = ["Banana", "Gava", "Graps", "Cherry"]
abc = ' '.join(fruits)
print(abc)
Output:
The will print the following string of characters.
Banana Gava Graps Cherry
List Splitting:
In python we can also split or divide lists into sublists. This split operation will result a new list that can be stored in new variable or can be used for any other computation in script. This technique is also called python list slicing.
Python list splitting syntax:
Syntax to divide a list into sublists is as:
list(start_index : end_index : step)
Here start index will be include in result but the last index will not include. If we specify the same index values for starting and ending index then resultant array/list will be empty.
Example:
nums = [2,4,6,8,10,12,14,16,18,20]
print(nums[1:3])
Output:
[4, 6]
Remove element from list:
For removing a single element from python list we can call list.remove() function. This will remove particular value. Parameter of the remove method is value not an index. Using this method we can easily delete element from list python.
Syntax:
list.remove(value)
List negative indexing:
In python programming we can also access elements from the list using negative indexing. This means we can acess list from last index to the first element. To access the last element we will use -1 similarly the second last elements negative index will be -2 and so on.
Python list methods:
Sr. | Method Name | Use |
1 | sort() | This will sort the list elements in ascending order. |
2 | reverse() | Reverse method is used for only reversing the list values. This will not sort in descending order. |
3 | count() | This is used to count occurance of particular value/item in the list. |
4 | index() | Index method will return the index of first matched value, provided as parameter. |
5 | clear() | This method will remove/delete all items from te list. |
6 | pop() | This pop method will retrive and remove last item from list. (Similar to stack pop operation) |
7 | remove() | This will remove provided value from list. |
8 | insert() | Insert method is used to insert item in list at the specified index. |
9 | extend() | This method will add elements from another array to the calling array. |
10 | append() | Append method add an element at the end of the list. |
Here is complete list of python methods that are used for different list operations.
Python list to tuple
In python we can easily convert/cast a list into tuple using cast operator.
Syntax:
tuple ( list_identifier)
Python arrays
Python lists are much similar to array in other high level programming languages. Where each element is accessible by their index value. Similar to other programming languages the first index of list element is also start from 0. We can also create nested list that are also known as python 2d array.