Description:
There are so many ways for doing this. Its depends on individual choice, whether you are comfortable
using loop or list comprehension, its your choice but there is lots of ways for doing this.
Using loop-->
Program:
somelist = [0,3,5,2,6,2,4]
print("Old List",somelist)
new_list = [x for x in somelist if x%2==0]
somelist = new_list
del(new_list)
print("New list:",somelist)Output:
Old List [0, 3, 5, 2, 6, 2, 4]
New list: [0, 2, 6, 2, 4]
>>>
Different way:
Program:
mylist = [1,2,3,4,5,6,7]
print("Old List",mylist)
newlist = [x for x in mylist if x%2 != 0]
print("New list:",newlist)
Output:
Old List [1, 2, 3, 4, 5, 6, 7]
New list: [1, 3, 5, 7]
>>>
Comments