Perform the following functions using lists:
1. Find whether all elements in list are numbers or not
2. If numeric list then count number of odd values in it
3. if string list then display the largest string in the list
4. if all elements are string then count numeric string and string with alphabets only
Answers
The following cσdes have been written using Python.
l = list()
n = int(input("Enter the number of elements to be added: "))
print()
for i in range(n):
x = eval(input("Enter the element: "))
l.append(x)
print()
#to find if all the elements are numerical/not
for i in l:
if type(i) == int:
r = "Numerical list"
else:
r = "String list"
print(r)
print()
#counting the number of odd values
c = 0
if r == "Numerical list":
for i in l:
if i%2 != 0:
c = c + 1
print("There are", c, "odd values in the list.")
print()
#retrieving the largest string from the list
elif r == "String list":
ls = max(l, key = len)
print("The largest string is: ", ls)
print()
#counting the number of numerical/alphabetical strings
if r == "String list":
ac = 0
nc = 0
for i in l:
if i.isalpha():
ac = ac + 1
elif i.isdigit():
nc = nc + 1
print("There are", ac, "alphabetical strings and", nc, "numerical strings.")