List in Python
List in Python
Python lists are versatile data structures that allow you to store multiple items in a single variable. They
are one of the four built-in data types in Python designed to store collections of data, with the others
being Tuple, Set, and Dictionary. Lists are ordered, mutable and also allow duplicate values. Elements in
the list are separated by comma and enclosed in single/double quotation marks (strings are enclosed
whereas integers are written without quotes). The whole list is enclosed in brackets [ ]
---------------------------------------------------------------------------------------------------------------------------------------------
1. append() function in list: This function is used to enter new elements in a list. This function simply takes
the value as argument and put the value as the last element of the list.
WAP in Python to create an empty list and ask the user to enter values in the list (using APPEND function)
WAP in Python to ask the user enter multiple new values in an already created list (using APPEND function).
WAP in Python to ask the user enter the index number and a value. Display the updated list with the elemet inserted
at the required index number.
WAP in Python to ask the user enter any index number and displaying the element at that index number from the
list.
4. clear() function in list: This function deletes all the elements from the list and displays the list as an
empty list.
5. sort() function in list: This function is used to arrange the elements of a list in ASCENDING or DESCENDING
order.
By default, this function sorts the list elements in ASCENDING order and for sorting in
DESCENDING order, (reverse=True) needs to be given as the argument of sort function.
Note: For using SORT function, the data in the list must be of the same type. Like,
integers or floating point numbers can’t be sorted with strings.
In the above examples, if we take the list elements as integers or floating point values, then it will be sorted
normally as per the values but, in the case of strings, sorting is done as per the ASCII value of the first character of
the string.
If you see the right hand side picture above, ‘r’ in the rabbit is having the highest ASCII value hence, it is displayed as
the maximum value. ‘D’ in dog is having the lowest ASCII value hence, it is displayed as the minimum value.
7. extend()function in list: This function is used to join one list after the another.
8. count() function in list: This function gives the number of an element’s occurance in the list. In other words, it
shows that how many times a particular element is there in the list. The required element
needs to be passed as argument to this function. If the argument contains a value which
is not present in the list, this function will display 0.
10. pop() function in list: This function removes an element from the list whose index number is given as
function argument. If no index number is given then, it removes the last element
of the list. Please see the example given below at the left hand side, nothing is
given as the argument of the pop() function so, it removed the last element
which is 125. In the example given at the right hand side, it removed 56.5 which
has the index number 2 because, 2 is given as the argument to the pop()
function.
11. remove() function in list: This function deletes the element from the list which is given as the argument to the
function. If, the function is present in the list more than once then, the element at
the first occurance will be deleted. You can see in the example below that an
element 125 is present two times in the list. So, 125 present at the first occurance is
deleted.