Check if List contains Item in Python

The challenge

Create a method that accepts a list and an item, and returns true if the item belongs to the list, otherwise false.

Test cases

list = [0,1,2,3,5,8,13,2,2,2,11];
Test.assert_equals(include(list, 100), False, "list does not include 100")
Test.assert_equals(include(list, 2), True, "list includes 2 multiple times")
Test.assert_equals(include(list, 11), True, "list includes 11")
Test.assert_equals(include(list, "2"), False, "list includes 2 (integer), not ''2'' (string)")
Test.assert_equals(include([], 0), False, "empty list doesn't include anything")
Test.assert_equals(include(list, 0), True, "list includes 0")

The solution in Python

def include(arr,item):
    return item in arr

Tags:
3.7 3 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
W.HatIsThis
W.HatIsThis
5 days ago

What a nice and helpful article..