Skip to main content

remove specific element in python list - remove index based element

 

remove specific element in python list

#remove specific element in python list

list=list([34,55,34,66,77,45.6,23,34,])

print(list)

list.remove(66)

print(list)

output:

[34, 55, 34, 66, 77, 45.6, 23, 34]

[34, 55, 34, 77, 45.6, 23, 34]


#Remove all occurrence of a specific python repeated element

mylist = list([26, 4, 26, 26, 8, 12])

for item in mylist:

    mylist.remove(26)

print(mylist)

output : 

[4,8,12]

#remove item present at given index

mylist = list([2, 4, 6, 8, 10, 12])

mylist.pop(2)  #remove 2 index based element

print(mylist)

output: 

[2, 4, 8, 10, 12]

# remove item without passing index number(last value remove) using pop()

mylist = list([2, 4, 6, 8, 10, 12])

mylist.pop()  

print(mylist)

output :

[2, 4, 6, 8, 10]

[2, 4, 8, 10]

#remove the range of elements python list with slice:

mylist = list([2, 4, 6, 8, 10, 12])

#remove item from index 2 to 5

del mylist[2:5]

print(mylist)

output:

[2, 4, 12]

#remove all items starting from index 2

mylist=list([23,44,55,34,53,78,55])

del mylist[2:]

print(mylist)

output:

[23,44]

#remove entair list using del keyword

mylist=list([23,44,55,34,53,78,55])

del mylist

print(mylist)

output:

mylist is not found 

Comments

Popular posts from this blog

Python Complex Datatypes

Python Complex Datatypes    A complex number is a number with a real and an imaginary component represented as a+bj where a and b contain integers or floating-point values. # both values are int type a = 9 + 8j           print(a)      print(type(a)) # one int and one float values b = 10 + 4.5j        print(b)       print(type(b)) # both values are float type c = 11.2 + 1.2j      print(c) print(type(c))

Python Opps Concepts: class and Object - Class and Objects in Python

Python Opps: class and Object Python Opps Concepts : Class and Object What is a Class and Objects in Python? Class: The class is a user-defined data structure that binds the data members and methods into a single unit. Class is a blueprint or code template for object creation. Using a class, you can create as many objects as you want Object: An object is an instance of a class. It is a collection of attributes (variables) and methods. We use the object of a class to perform actions.  #print name and age input from user using python object and class  EX: class person:     name=input("enter name")     age=input("enter age")     def __init__(self,name,age):         self.name=name         self.age=age     def greet(self):         #print("name ",self.name,"age",self.age)         print(f"{self.name} is {self.age} years old") a=person(person.name,person.age) a.greet...

Adding and changing elements in a tuple using insert() and append methods

  Adding and changing elements in a tuple using insert and append: A list is a mutable type, which means we can add or modify values in it, but tuples are immutable, so they cannot be changed. #how to add new element in tuple object using index based tup=(6,75,66,34,23) print(tup) #print all tuple elements newlist=list(tup) #tuple to list convert newlist.insert(2,40) #add new element in list tup=tuple(newlist) #convert list to tuple print(tup) #print all elements in tuple output: (6, 75, 66, 34, 23) (6, 75, 40, 66, 34, 23) #how to add new element in tuple object using apped() method tup=(6,75,66,34,23) newlist=list(tup) newlist.append(22) tup=tuple(newlist) print(tup) output: (6, 75, 66, 34, 23, 22) #Modify nested items of a tuple: then we can change its values in the case of a nested tuple. Ex: tuple1 = (10, 20, [25, 75, 85]) print(tuple1) tuple1[2][0] = 250 print(tuple1) output: (10,20,[250,75,85])      #python tuples, #python training in guntur, #python jobs in gu...