Function in Python that filters integers or strings from a given list

    
# returns integers only from a list
   def onlyints(string):
    x = list(filter(lambda e: isinstance(e, int), string))
    return x

mylist = [3,6,"abdur", "space", 767, 555, 678]
print(onlyints(mylist))

    
    
# returns strings only from a list
   def onlyints(string):
    x = list(filter(lambda e: isinstance(e, str), string))
    return x

mylist = [3,6,"abdur", "space", 767, 555, 678]
print(onlyints(mylist))

    
    
# returns integers only from a list
   def onlyints(string):
    return [e for e in string if isinstance(e, int)]

mylist = [3,6,"abdur", "space", 767, 555, 678]
print(onlyints(mylist))

    
    
# returns strings only from a list
   def onlyints(string):
    return [e for e in string if isinstance(e, str)]

mylist = [3,6,"abdur", "space", 767, 555, 678]
print(onlyints(mylist))

    

Comments

Popular posts from this blog

Quotation marks to wrap an element in HTML

What is the difference between iostream and iostream.h in cpp?

The Basic Structure of a Full-Stack Web App