list
Since list is iterable and programming concept of iterator is implemented in list, concept of for loop is applicable on the list.
#creating list objects
As list elements are iterable, we can apply for loop and while loop as below
List index works both direction -1 index will fetch last element and -2 will access second last and so on but if there are no index available, we get error.
#how to delete element
del listname[index]
#how to update element
listname[index] = element
Remember if index is out of defined range, we get index of range error.
List is growable -> since it is a class, it has predefined methods to update the size. Can be achieved with append to add at last. To access member function we can use (.) operator.
Syntax - Object.memberFunction()
Methods in list
extend - To extend existing list with other iterable
sort- To sort list
remove - To remove element
reverse - To reverse list
Packing and upacking in list -> To add elements into the list and distribute element from list to individual elements. Very useful concept, number of variable must be equal while unpacking else we will get error.
Built in function - This method can be applied on any iterable
max(), min(), sorted(),len(),sum()
Help on built-in function sorted in module builtins:
sorted(iterable, /, *, key=None, reverse=False)
Return a new list containing all items from the iterable in ascending order.
A custom key function can be supplied to customize the sort order, and the
reverse flag can be set to request the result in descending order.
Comments
Post a Comment