Informatics Practices
Define the following functions:
(a) len()
(b) append()
(c) extend()
(d) pop()
(e) remove()
(f) del
(g) sort()
(h) sorted()
(i) copy()
(j) count()
(k) max()
(I) min()
(m) sum()
Python List Manipulation
2 Likes
Answer
(a) len() — The len()
function returns the length of the list, i.e., the number of elements in a list.
(b) append() — The append()
method adds a single item to the end of the list. It does not create a new list, rather it modifies the original list.
(c) extend() — The extend()
method adds one list at the end of another list. In other words, all the items of a list are added at the end of an already created list.
(d) pop() — The pop()
method removes the element from the specified index and also returns the element which was removed.
(e) remove() — The remove()
function removes the first occurrence of the item from the list. It is useful when we know the element to be deleted but not the index of the element.
(f) del — The del
statement removes the specified element from the list but does not return the deleted value.
(g) sort() — The sort()
function sorts the items of the list, by default in ascending/increasing order. To sort the list in descending order we use reverse = True
parameter. Sorting is done 'in place', i.e., it does not create a new list.
(h) sorted() — The sorted()
method takes a list as a parameter and creates a new list consisting of the same elements but arranged in ascending order and returns it as a list. To sort the list in descending order we use reverse = True
parameter.
(i) copy() — The copy()
function returns a new list stored in a different memory location.
(j) count() — The count()
method counts how many times an element has occurred in a list and returns it.
(k) max() — The max()
method returns the element with the maximum value from the list.
(I) min() — The min()
method returns the element with the minimum value from the list.
(m) sum() — The sum()
function in Python is used to calculate the sum of all the elements in list.
Answered By
1 Like
Related Questions
What are the similarities between strings and lists?
Why are lists called a mutable data type?
What is the difference between insert() and append() methods of a list?
Suppose L = [10, ["few", "facts", "fun"], 3, 'Good']
Consider the above list and answer the following:
(a) L[3:]
(b) L[::2]
(c) L[1:2]
(d) L[1] [1]