Check if List contains Item in Python

0 min read 85 words

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:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags