0% found this document useful (0 votes)
2 views

List in Python

The document provides an overview of Python lists, highlighting their characteristics such as being ordered, mutable, and allowing duplicates. It details various list functions including append(), insert(), copy(), clear(), sort(), min(), max(), extend(), count(), index(), pop(), remove(), and reverse(), along with examples of their usage. The document serves as a guide for understanding and manipulating lists in Python programming.

Uploaded by

Lakshya Siwatch
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

List in Python

The document provides an overview of Python lists, highlighting their characteristics such as being ordered, mutable, and allowing duplicates. It details various list functions including append(), insert(), copy(), clear(), sort(), min(), max(), extend(), count(), index(), pop(), remove(), and reverse(), along with examples of their usage. The document serves as a guide for understanding and manipulating lists in Python programming.

Uploaded by

Lakshya Siwatch
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

LISTS 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 append a new element in an already created list.

WAP in Python to ask the user enter multiple new values in an already created list (using APPEND function).

SUDHIR SANWAL Page 1 of 6


2. insert() function in list: This function is also used to enter new elements in a list. This function takes two
arguments, index number and the value. The new element is placed the
designated index number. If the index number is given a value which is exceeding
the length of the list, then the value will be added at the last of the list.

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.

SUDHIR SANWAL Page 2 of 6


3. copy() function in list: This function makes a copy of a list as another list. In the example given below,
you can see that L is a list and its copy has been made in M. Now, both the lists
L and M will show the same contents.

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.

SUDHIR SANWAL Page 3 of 6


6. min() and max() functions in list: These functions are used to identify the minimum and maximum
values from the list elements. At the time of using these functions, the
data type of the list elements must be same.

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.

SUDHIR SANWAL Page 4 of 6


9. index() function in list: This function displays the index number of an element present in the list. As you know
that list allows duplicate values also so, if such element is given to find out the index, it
will show the index number of the first occurance of the element. If you see the picture
below, an element 125 is present in the list at 2 places i.e. at index no. 1 and index no.
4 so, this function has shown its position at the first occurance only, which is index 1.

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.

SUDHIR SANWAL Page 5 of 6


12. reverse() function in list: This function reverses the order of the original list.

SUDHIR SANWAL Page 6 of 6

You might also like