Python Lists
List in python is implemented to store the sequence of various type of data. However, python contains six data types that are capable to store the sequences but the most common and reliable type is list. 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 []. A list can be defined as follows.
If we try to print the type of L1, L2, and L3 then it will come out to be a list. Lets consider a proper example to define a list and printing its values.
Output: printing employee data... Name : John, ID: 102, Country: USA printing departments... Department 1: Name: CS, ID: 11 Department 2: Name: IT, ID: 11 HOD Details .... CS HOD Name: Mr. Holding, Id: 10 IT HOD Name: Mr. Bewon, Id: 11 <class 'list'> <class 'list'> <class 'list'> <class 'list'> <class 'list'> List indexing and splittingThe 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 and goes to length - 1. 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. Consider the following example. Unlike other languages, 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. Updating List valuesLists are the most versatile data structures in python since they are immutable and their values can be updated by using the slice and assignment operator. Python also provide us the append() method which can be used to add values to the string. Consider the following example to update the values inside the list.
Output: [1, 2, 3, 4, 5, 6] [1, 2, 10, 4, 5, 6] [1, 89, 78, 4, 5, 6] The list elements can also be deleted by using the del keyword. Python also provides us the remove() method if we do not know which element is to be deleted from the list. Consider the following example to delete the list elements.
Output: [0, 1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3] Python List OperationsThe concatenation (+) and repetition (*) operator work in the same way as they were working with the strings. Lets see how the list responds to various operators.
Iterating a ListA list can be iterated by using a for - in loop. A simple list containing four strings can be iterated as follows.
Output: John David James Jonathan Adding elements to the listPython provides append() function by using which we can add an element to the list. However, the append() method can only add the value to the end of the list. Consider the following example in which, we are taking the elements of the list from the user and printing the list on the console.
Output: Enter the number of elements in the list 5 Enter the item?1 Enter the item?2 Enter the item?3 Enter the item?4 Enter the item?5 printing the list items.... 1 2 3 4 5 Removing elements from the list
Output: printing original list: 0 1 2 3 4 printing the list after the removal of first element... 1 2 3 4 Python List Built-in functionsPython provides the following built-in functions which can be used with the lists.
Python List built-in methods
|