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))
Python
# 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))
Python
# 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))
Python
# 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))
Python

Comments

Popular posts from this blog

Quotation marks to wrap an element in HTML

Making GUI Calculator in Tkinter Python

Unlocking Web Design: A Guide to Mastering CSS Layout Modes