Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

delete element of a list from another list python

l1 = ["a", "b", "c", "d", "e", "f"]
l2 = ["b", "c", "e"]

l1 = [elt for elt in l1 if elt not in l2]
# l1 = ['a', 'd', 'f']
Comment

python delete element from list

# Basic syntax:
my_list.remove(element) # or:
my_list.pop(index)
# Note, .remove(element) removes the first matching element it finds in
# 	the list.

# Example usage:
animals = ['cat', 'dog', 'rabbit', 'guinea pig', 'rabbit']
animals.remove('rabbit')
print(animals)
--> ['cat', 'dog', 'guinea pig', 'rabbit']
# Note only the first instance of rabbit was removed from the list. 

# Note, if you want to remove all instances of an element (and it's the only
#	duplicated element), you could convert the list to a set then back to a
#	list, and then run .remove(element)	E.g.:
animals = list(set['cat', 'dog', 'rabbit', 'guinea pig', 'rabbit'])
animals.remove('rabbit')
print(animals)
--> ['cat', 'dog', 'guinea pig']
Comment

how delete element from list python

myList = ["Bran", 11, 33,"Stark"] 
del myList[2] # output:  [‘Bran’, 11, ‘Stark’]
Comment

PREVIOUS NEXT
Code Example
Python :: favicon django 
Python :: celsius to fahrenheit in python 
Python :: how to get input in tkinter 
Python :: python random 
Python :: python access index in for loop 
Python :: write dataframe to csv python 
Python :: record video with python 
Python :: save image requests python 
Python :: reverse dictionary python 
Python :: code for showing contents of a file and printing it in python 
Python :: update jupyter notebook 
Python :: godot white shader 
Python :: multiple variable input in python 
Python :: genspider scrapy 
Python :: how to update python in linux 
Python :: py sleep function 
Python :: pyton read text file 
Python :: how to migrate from sqlite to postgresql django 
Python :: rename column name pandas dataframe 
Python :: valueerror expected 2d array got 1d array instead python linear regression 
Python :: how to clear console in repl.it python 
Python :: find root directory of jupyter notebook 
Python :: numpy remove rows containing nan 
Python :: tkinter python may not be configured for Tk 
Python :: how to save a model fast ai 
Python :: Print Table Using While Loop In Python 
Python :: python get all images in directory 
Python :: how to edit a specific line in text file in python 
Python :: python get time milliseconds 
Python :: cv2 show image 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =