1 From the given list given below
gadgets = [“Mobile”, “Laptop”, 100, “Camera”, 310.28, “Speakers”, 27.00,
“Television”, 1000, “Laptop Case”, “Camera Lens”]
Write a python program to :
a)Create separate lists of strings and numbers by checking each element of the list.
b)Sort the strings list in ascending order.
c)Add an element 56.89 to the number list.
d)Replace the element “Speakers” to “Headphones”
e)Delete the 2nd element of the numbers list
f) Display the modified strings and number lists.
Answers
Given list:
gadgets = [“Mobile”, “Laptop”, 100, “Camera”, 310.28, “Speakers”, 27.00, “Television”, 1000, “Laptop Case”, “Camera Lens”]
a) Creating a separate list of strings and numbers by checking each element of the list.
gadgets = ["Mobile", "Laptop", 100, "Camera", 310.28, "Speakers", 27.00, "Television", 1000, "Laptop Case", "Camera Lens"]
sl = list()
nl = list()
for i in gadgets:
if type(i) == str:
sl.append(i)
else:
nl.append(i)
print(sl, "is the list of string values.")
print(nl, "is the list of numerical values.")
b) Sorting the strings list in ascending order.
This is done with the view that it proceeds from the above coding. If not, a program will have to be made to create a list solely for string, something similar to the one above, excluding the numerical part.
sl.sort()
print(sl)
c) Adding an element 56.89 to the number list.
nl.append(56.89)
print(nl)
d) Replacing the element "Speakers" to "Headphones".
sl.replace("Speakers", "Headphones")
print(sl)
e) Deleting the second element of the numbers list.
del nl[1]
print(nl)
f) Displaying the modified strings and number lists.
print(sl, "is the new list of strings.")
print(nl, "is the new list of numbers.")