List Functions
List methods are discussed in this articles.
1. “in” operator :- This operator is used to check if an element is present in the list or not. Returns true if element is present in list else returns false.
2. “not in” operator :- This operator is used to check if an element is not present in the list or not. Returns true if element is not present in list else returns false.
Output:
List is having element with value 4 List is having element with value 4
3. len() :- This function returns the length of list.
4. min() :- This function returns the minimum element of list.
5. max() :- This function returns the maximum element of list.
Output:
The length of list is : 5 The minimum element of list is : 1 The maximum element of list is : 5
6. “+” operator :- This operator is used to concatenate two lists into a single list.
7. “*” operator :- This operator is used to multiply the list “n” times and return the single list.
Output:
list after concatenation is : 1 2 3 4 5 6 list after combining is : 1 2 3 1 2 3 1 2 3
8. index(ele, beg, end) :- This function returns the index of first occurrence of element after beg and before end.
9. count() :- This function counts the number of occurrences of _arrow
Output:
The first occurrence of 3 after 3rd position is : 5 The number of occurrences of 3 is : 2
More methods are discussed in this article.
1. del[a : b] :- This method deletes all the elements in range starting from index ‘a’ till ‘b’ mentioned in arguments.
2. pop() :- This method deletes the element at the position mentioned in its arguments.
Output:
List elements after deleting are : 2 1 3 8 List elements after popping are : 2 1 83. insert(a, x) :- This function inserts an element at the position mentioned in its arguments. It takes 2 arguments, position and element to be added at respective position.
4. remove() :- This function is used to delete the first occurrence of number mentioned in its arguments.
Output:
List elements after inserting 4 are : 2 1 3 4 5 3 8 List elements after removing are : 2 1 4 5 3 85. sort() :- This function sorts the list in increasing order.
6. reverse() :- This function reverses the elements of list.
Output:
List elements after sorting are : 1 2 3 3 5 8 List elements after reversing are : 8 5 3 3 2 17. extend(b) :- This function is used to extend the list with the elements present in another list. This function takes another list as its argument.
8. clear() :- This function is used to erase all the elements of list. After this operation, list becomes empty.
Output:
List elements after extending are : 2 1 3 5 6 4 3 List elements after clearing are :
Comments
Post a Comment