174k views
2 votes
Write a function that takes, as an argument, a list, identified by the variable aList. If the list only contains elements containing digits (either as strings as non-negative integers), return the string formed by concatenating all of the elements in the list (see the example that follows).

User Madesch
by
6.7k points

1 Answer

1 vote

Answer:

The function is as follows:

def concList(aList):

retList = ""

for i in aList:

if(str(i).isdigit()):

retList+=str(i)

else:

retList = "Not digits"

break;

return retList

Step-by-step explanation:

This defines the function

def concList(aList):

This initializes the return string to an empty string

retList = ""

This iterates through aList

for i in aList:

This converts each element of the list to an empty list and checks if the string is digit

if(str(i).isdigit()):

If yes, the element is concatenated

retList+=str(i)

If otherwise

else:

The return string is set to "No digits"

retList = "Not digits"

And the loop is exited

break;

This returns the return string

return retList

User Nibot
by
7.7k points