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.

# Python code to demonstrate the working of
# "in" and "not in" 
# initializing list
lis = [1, 4, 3, 2, 5]
  
# checking if 4 is in list using "in"
if 4 in lis:
        print ("List is having element with value 4")
else print ("List is not having element with value 4")
  
# checking if 4 is not list using "not in"
if 4 not in lis:
        print ("List is not having element with value 4")
else print ("List is having element with value 4")

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.

filter_none

edit

play_arrow

brightness_4

# Python code to demonstrate the working of
# len(), min() and max()
# initializing list 1
lis = [2, 1, 3, 5, 4]
  
# using len() to print length of list
print ("The length of list is : ", end="")
print (len(lis))
  
# using min() to print minimum element of list
print ("The minimum element of list is : ", end="")
print (min(lis))
  
# using max() to print maximum element of list
print ("The maximum element of list is : ", end="")
print (max(lis))

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.

filter_

edit

play_arrow

brightness_4

# Python code to demonstrate the working of
# "+" and "*"
# initializing list 1
lis = [1, 2, 3]
  
# initializing list 2
lis1 = [4, 5, 6]
  
# using "+" to concatenate lists
lis2= lis + lis1
  
# priting concatenated lists
print ("list after concatenation is : ", end="")
for i in range(0,len(lis2)):
         print (lis2[i], end=" ")
           
print ("\r")
  
#using '*' to combine lists 
lis3 = lis * 3
  
# priting combined lists
print ("list after combining is : ", end="")
for i in range(0,len(lis3)):
         print (lis3[i], end=" ")

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

brness_4

# Python code to demonstrate the working of
# index() and count()
# initializing list 1
lis = [2, 1, 3, 5, 4, 3]
  
# using index() to print first occurrence of 3
# prints 5
print ("The first occurrence of 3 after 3rd position is : ", end="")
print (lis.index(3, 3, 6))
  
# using count() to count number of occurrence of 3
print ("The number of occurrences of 3 is : ", end="")
print (lis.count(3))

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.



filter_none

edit

play_arrow

brightness_4

# Python code to demonstrate the working of
# del and pop()
  
# initializing list 
lis = [2, 1, 3, 5, 4, 3, 8]
  
# using del to delete elements from pos. 2 to 5
# deletes 3,5,4
del lis[2 : 5]
  
# displaying list after deleting 
print ("List elements after deleting are : ",end="")
for i in range(0, len(lis)):
    print(lis[i], end=" ")
      
print("\r")
  
# using pop() to delete element at pos 2
# deletes 3
lis.pop(2)
  
# displaying list after popping  
print ("List elements after popping are : ", end="")
for i in range(0, len(lis)):
    print(lis[i], end=" ")

Output:

List elements after deleting are : 2 1 3 8 
List elements after popping are : 2 1 8 

3. 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.

filter_none

edit

play_arrow

brightness_4

# Python code to demonstrate the working of
# insert() and remove()
  
# initializing list 
lis = [2, 1, 3, 5, 3, 8]
  
# using insert() to insert 4 at 3rd pos
lis.insert(3, 4)
  
# displaying list after inserting
print("List elements after inserting 4 are : ", end="")
for i in range(0, len(lis)):
    print(lis[i], end=" ")
      
print("\r")
  
# using remove() to remove first occurrence of 3
# removes 3 at pos 2
lis.remove(3)
  
# displaying list after removing 
print ("List elements after removing are : ", end="")
for i in range(0, len(lis)):
    print(lis[i], end=" ")

Output:

List elements after inserting 4 are : 2 1 3 4 5 3 8 
List elements after removing are : 2 1 4 5 3 8 

5. sort() :- This function sorts the list in increasing order.

6. reverse() :- This function reverses the elements of list.

filter_none

edit

play_arrow

brightness_4

# Python code to demonstrate the working of
# sort() and reverse()
  
# initializing list 
lis = [2, 1, 3, 5, 3, 8]
  
# using sort() to sort the list
lis.sort()
  
# displaying list after sorting
print ("List elements after sorting are : ", end="")
for i in range(0, len(lis)):
    print(lis[i], end=" ")
      
print("\r")
  
# using reverse() to reverse the list
lis.reverse()
  
# displaying list after reversing
print ("List elements after reversing are : ", end="")
for i in range(0, len(lis)):
    print(lis[i], end=" ")

Output:

List elements after sorting are : 1 2 3 3 5 8 
List elements after reversing are : 8 5 3 3 2 1 

7. 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.

filter_none

edit

play_arrow

brightness_4

# Python code to demonstrate the working of
# extend() and clear()
  
# initializing list 1
lis1 = [2, 1, 3, 5]
  
# initializing list 1
lis2 = [6, 4, 3]
  
# using extend() to add elements of lis2 in lis1
lis1.extend(lis2)
  
# displaying list after sorting
print ("List elements after extending are : ", end="")
for i in range(0, len(lis1)):
    print(lis1[i], end=" ")
      
print ("\r")
  
# using clear() to delete all lis1 contents
lis1.clear()
  
# displaying list after clearing
print ("List elements after clearing are : ", end="")
for i in range(0, len(lis1)):
    print(lis1[i], end=" ")

Output:

List elements after extending are : 2 1 3 5 6 4 3 
List elements after clearing are : 

Comments

Popular posts from this blog

What is Python?

String Slicing And Other Functions

Lists And Tuples