In: Computer Science
Create two lists; one contains 5 elements and the other one is empty. Write a python program that iterates through the first one, pops the elements from the end of it, then it pushes them one by one to the empty list. As your program iterates through the first list, clean it before processing the data, meaning if any element is like a special character ( , . ; : ) it must be discarded and not gets pushed to the second list.
Python Code pasted below
#first list with 5 items
list1=[1,"apple",":","mango",","]
#second list is empty
list2=[]
print("First List before the operation:",list1)
print("Second List before the operation:",list2)
#Taking each elemnets from first list from right most end till last
element
for i in range(len(list1)-1,-1,-1):
#pops out item from list1
item=list1.pop(i)
#checks whether it is a special character.
#If special character, then go to the next iteration
if item =="," or item=="." or item==";" or item==":":
continue
#Adds the poped item to second list
list2.append(item)
#prints both the lists
print("First List after the operation:",list1)
print("Second List after the operation:",list2)
Python code in IDLE pasted for better understanding of the indent
Output Screen