LISTS in Python




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

Empty_List = []

type (Empty_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


List_of_Int = [1,2,3,4,5,6,7,8,9,10] #Creating a List of INT Values

print (List_of_Int) #Print the 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


List_of_string = ['a','b','c','d','e','f']

print (List_of_string) #Will Print out all items in the list

 


List elements can consist of any DataTypes The below list has an Int Type (1), A String (Tom), a Float (3.1412), and a Boolean (TRUE)



Accessing Items in the 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


Since there is no element at the Fifth Index we will get a "List index out of range error

Updating Items in the LIST

If we want to  Update the value of an element in a list, we can do it by specifying the index and assigning a new value to it as we have done in the example below.


Adding Items to 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 to add an element to a specific index location we can use the insert function in the list, Suppose I want  'Ship' to be the second element of the list I can provide the index location.


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").



Removing Items from a LIST


location We can use the remove function to remove an element from a list, As shown below we are using the remove function to remove an element 'Bus' from a list


You can use the pop() function to remove the last element from the list. in the below example, the first element of the list is 'Airplane' if we use the pop() function it will remove 'Airplane' from the list
 


We can also sort the items in the list by using the sort() 
command 

We can also sort the items in the list by using the reverse() command 


We can also add two lists together by simply adding the two lists, we can use the + Operator to add two lists together



You can use the clear command to clear all the elements in the list


To Delete the List completely from the memory we can use the del key


We can also use the assignment operator (=) to create a Shallow copy of a list 


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





No comments: