In: Computer Science
I am stuck on the following problem please and it has to be in python!
1) Initially, create a list of the following elements and assign the list to a variable "thing".
"Mercy", "NYU", "SUNY", "CUNY"
2) print the list above
3) add your last name to the list
4) print the list
5) add the following elements as a nested list to the list:
"iPhone", "Android"
6) print the list
7) add the following list to the end of the list as elements:
["MIT", "CMU"]
8) print the list
9) add the following nested list to the third position of the list thing:
[30, 40+1]
10) print the list
11) delete "SUNY" from the list. # revised this question on 9/24 8:41pm!
12) print the list
13) add "Mercy" to the second last in the list (so "Mercy" should be in between "MIT" and "CMU"):
14) print the list
15) count "Mercy" in the list and print.
16) print the number of the top-level elements in the list.
17) In Step 10 above, explain why 40+1 is entered 41 to the list.
Challenge) Can we remove an element from a nested list? If yes, explain how in plain text. If not, explain why not
Challenge) Can we count each every element in a list, which may contain nested lists? If yes, explain how in plain text. If not, explain why not
That's a lot of questions.
Howwver, I have solved all of them.
please follow the beloe steps.
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly indented for
better understanding.
# 1. create initial list
thing = ["Mercy", "NYU", "SUNY", "CUNY"]
# 2. print the list
print(thing)
# 3. add your name to the list
# Replace XYZ with your name
thing.append('XYZ')
# 4. print the list again
print(thing)
# 5. add elements as nested list
thing.append(["iPhone", "Android"])
# 6. print the list again
print(thing)
# 7. add list to end
thing.append(["MIT", "CMU"])
# 8. print the list again
print(thing)
# 9. add list at 3rd position
thing.insert(3, [30, 40 + 1])
# 10. print the list
print(thing)
# 11. delete SUNY from list
thing.remove("SUNY")
# 12. print the list
print(thing)
# 13. add Mercy to the second last in list
thing.insert(-2, "Mercy")
# 14. print the list
print(thing)
# 15.count Mercy and print
print(thing.count("Mercy"))
# 16. Number of top level elements
print(len(thing))
# 17
# Here 40+1 = 41 this operation will be performed and result of 41 will be added instead of 40+1
# Challenge1: Yes, we can remove elements in nested list.
# Ex: x=[ [1,2], [4,5] ]
# To remove 5, we use delx[1][1]
# Challenge2: Yes, we can do it using NESTED LOOPS
===============
SCREENSHOTS:


