Question

In: Computer Science

Create two lists; one contains 5 elements and the other one is empty. Write a python...

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.

Solutions

Expert Solution

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


Related Solutions

There are 5 boxes. One box is empty; two boxes each contains one marble; and the...
There are 5 boxes. One box is empty; two boxes each contains one marble; and the remaining two boxes each contains two marbles. Suppose that the colors of the marbles are not known, but it is known that each marble can only be either red or green, with equal probability. Suppose that ONE box is randomly chosen from the 5 boxes, and let R denote the number of the red marble(s) the box has, and G be the number of...
Description of the Assignment: Write a Python program to (a) create a new empty stack. Then,...
Description of the Assignment: Write a Python program to (a) create a new empty stack. Then, (b) add items onto the stack. (c) Print the stack contents. (d) Remove an item off the stack, and (e) print the removed item. At the end, (f) print the new stack contents: Please use the following comments in your python script: # ************************************************* # COP3375 # Student's full name ( <<< your name goes here) # Week 8: Assignment 1 # ************************************************* #...
Given two lists, write python code to print “True” if the two lists have at least...
Given two lists, write python code to print “True” if the two lists have at least one common element. For example, x = [1,2,3], y=[3,4,5], then the program should print “True” since there is a common element 3.
Write a Python program to (a) create a new empty stack. Then, (b) add items onto...
Write a Python program to (a) create a new empty stack. Then, (b) add items onto the stack. (c) Print the stack contents. (d) Remove an item off the stack, and (e) print the removed item. At the end, (f) print the new stack contents:
Two capacitors are identical, except that one is empty and the other is filled with a...
Two capacitors are identical, except that one is empty and the other is filled with a dielectric (κ = 4.69). The empty capacitor is connected to a 11.0-V battery. What must be the potential difference across the plates of the capacitor filled with a dielectric so that it stores the same amount of electrical energy as the empty capacitor?
In python i want to create a function. The function will take in two linked lists...
In python i want to create a function. The function will take in two linked lists as the parameters. If one is shorter than the other then the shorter will be the length. I want to take the values from both linked lists and turn them into tuples. I then want these tuples to be put into a new linked list. I want to return that linked list. I want to do this using recursion and no helper functions or...
Python Programming Problem: If I have to separate lists, one contains a large string of paragraphs...
Python Programming Problem: If I have to separate lists, one contains a large string of paragraphs of words, and one contains just words, how can i iterate the words in my second list and compare it to my paragraph list to see how many times that word has occurred? List1 = ['paragraph.......'] List2 = ['words', 'words', 'words'......] these are just minimal examples how do i approach this problem? apprently numpy helps with processing time for something like this? cuz the...
Description of the Assignment: Write a Python program to create a deque and append few elements...
Description of the Assignment: Write a Python program to create a deque and append few elements to the left and right, then remove some elements from the left, right sides and reverse the deque. Please use the following comments in your python script: # ************************************************* # COP3375 # Student's full name ( <<< your name goes here) # Week 9: Assignment 1 # ************************************************* # Write your code below: # Create a deque # Print a deque # Append to...
Python please! Create one program to include all 10 elements: Create a function that prints a...
Python please! Create one program to include all 10 elements: Create a function that prints a sentence passed to the function as parameter. On the next line, print the same sentence without the 1st and the last character, use string slicing. Use the following sentence: This is the sentence. Given a string: This string was copied in an article. Replace the relevant words to read as follows: This string was discovered in the article xyz. Given a string: This string...
For this lab, you will work with two-dimensional lists in Python. Do the following: Write a...
For this lab, you will work with two-dimensional lists in Python. Do the following: Write a function that returns the sum of all the elements in a specified column in a matrix using the following header: def sumColumn(matrix, columnIndex) Write a function display() that displays the elements in a matrix row by row, where the values in each row are displayed on a separate line. Use a single space to separate different values. Sample output from this function when it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT