Removing All Instances of Specific Values from a List | Python Examples

#JavaInspires

 Hi Guys,

In this post, we will see python example to remove all instances of a value from a list.

Here, we will take a flower list with rose repeated several times.

To remove all instances of 'rose' we use while loop until 'rose' is no longer in the list.


Code :

#Removing All Instances of Specific Values from a List

flower_list=['rose','lily','tulip',
				'rose','lavender','sunflower',
				'rose']

print(flower_list)
#remove all rose in flower list
while 'rose' in flower_list:
	flower_list.remove('rose')
	
print()
print(flower_list)


Output :


['rose', 'lily', 'tulip', 'rose', 'lavender', 'sunflower', 'rose']

['lily', 'tulip', 'lavender', 'sunflower']



#JavaInspires

Post a Comment

Previous Post Next Post