Skip to content

Instantly share code, notes, and snippets.

@ashokbharat
Created May 29, 2017 10:17
Show Gist options
  • Save ashokbharat/6c645de7ddf1812e961a853927aa0b9f to your computer and use it in GitHub Desktop.
Save ashokbharat/6c645de7ddf1812e961a853927aa0b9f to your computer and use it in GitHub Desktop.
List Example
'''Take a list, say for example this one:
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
and write a program that prints out all the elements of the list that are less than 5.'''
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
new_list = []
for i in a:
if(i >= 5):
continue
else:
new_list.append(i)
print(new_list)
'''list_omprehension lambda is used for functional programming and filter to check for conditions and map is used for no condition check while list ietration'''
print (list(filter(lambda x:(x<5),a)))
'''Ask the user for a number and return a list that contains only elements from the original list a that are smaller than that number given by the user.'''
num = int(input("Enter a number which you want the list of elements smaller than this"))
print(list(filter(lambda x:(x<num),a)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment