A List is a complex Data Type in Python, A list is used to store a sequence of Elements, it is a list of Sorted Objects, Please note that the list can contain Duplicate values. a list is denoted by Square Brackets [].
Below we have created an Empty list, A list with an empty Square Bracket is an empty list, we can add elements to the list later.
Empty_List = []
If we try to print the TYPE of the list variable then we will get the output as a list
Below is an example of a LIST with Integer Values, we are creating a Variable "List_of_Int" and then we print the values of that list
Below is an example of a LIST with String Values, we are creating a Variable "List_of_string" and then we print the values of that list
To Access the first element in a List we can Specify the Index values [0] to get the First element in a List.
To Access the Second element in a List we can Specify the Index values [1] to get the Second element in a List.
Likewise, if we want to access the Last element in the list we can Specify the Index values [-1] to get the Last element in a List. and [-2] to get the Second Last element in a List
We can use the append function to add an item to the List, As shown below we are using the append function to add a new item "Ship" to our list. Please note that by using the append function the new element will be added at the very end of the list.
If we want the index location of an element in a list we can use the index
function, For example, if we want to check the index location of "Bus" I can use Means_of_Transportation.index("Bus").
Please note that if we add an element to a Shallow Copy then that change will be reflected in the original list as well Also if we delete an element from the Shallow Copy list then that change will be reflected in the original list as well
0 Comments