Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how to find whether 5 is in the list of array in python

+1 vote
asked Nov 15, 2019 by anonymous

2 Answers

+1 vote
answered Nov 15, 2019 by gameforcer (2,990 points)

You need to use key word "in". This allows you to check if something is in tuple, list, dictionary or set.

Examples:

sample_lst = [1,2,3,4,5]

print(5 in sample_lst)

 sample_lst = [1,2,3,4,5]

if 5 in sample_lst:

    #some code 

 

If you want to look for it in something like a list of lists you can do

2dlist = [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]]

print(any(6 in sublist for sublist in l2))

2dlist = [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]]

if any(5 in sublist for sublist in 2dlist):

    #some code

0 votes
answered Nov 19, 2019 by anonymous
for x in arr:

     if x == 5:

          return true

or

if 5 in arr:

   return true
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...