Menu Bar

Horizontal Scrollable Menu

ads

Search This Blog

Insert method , Remove Data From List , Remove Item From Position or from index , Remove method , Delete statement , Looping in list In Python

Insert method

Python list method insert () inserts object obj
 into the insert list.
Syntax

Following is the syntax to insert () method -

List. List (index, obj)

Parameter

     Index - This is the index where the object 
object needs to be inserted.

     obj - This is the item to be inserted in the
 given list.

return value

This method does not return any value, but it
 inserts the given element into the given index.
mixed.insert(0,'zero')

print(mixed)

Remove Data From List

The remove () method removes the object passed as
 an argument. If you need to remove elements based
 on the index (such as the fourth element or the 
last element), you can use the pop () method. In
addition, you can use the Dell statement to remove
items from a list or to delete an entire list.

popped=mixed.pop()

print(popped)            # OutPut = 9

print(mixed)            
 # OutPut = [1, 2, 3, [4, 5, 6], 'seven', 8.0]

Remove Item From Position or from index

example:

popped = mixed.pop(4)

print(popped)              # outPut = seven

print(mixed)
# OutPut = [1, 2, 3, [4, 5, 6], 8.0, 9]

Remove method

example:
print(mixed)                
# OutPut = [1, 2, 3, [4, 5, 6], 'seven', 8.0, 9]

mixed.remove('seven')      # OutPut = seven

print(mixed)  
 # OutPut = [1, 2, 3, [4, 5, 6], 8.0, 9]

Delete statement

# delete method is use to delete the element 
present in list

del mixed[6]

print(mixed)                   # OutPut = 9

Looping in list


for i in mixed:
    print(i)

    OUtPut
123[4, 5, 6]
seven
8.09

No comments:

ads3