Home > other >  How to create a function that search a list for a value that can be contained in a variable called k
How to create a function that search a list for a value that can be contained in a variable called k

Time:11-30

Write a function called find that will take a list of numbers, my_list, along with one other number, key. Have it search the list for the value contained in key. Each time your function finds the key value, print the array position of the key. You will need to juggle three variables, one for the list, one for the key, and one for the position of where you are in the list.

Copy/paste this code to test it:

my_list = [36, 31, 79, 96, 36, 91, 77, 33, 19, 3, 34, 12, 70, 12, 54, 98, 86, 11, 17, 17]
find(my_list, 12)
find(my_list, 91)
find(my_list, 80)

check for this output:

Found 12 at position 11
Found 12 at position 13
Found 91 at position 5

Use a for loop with an index variable and a range. Inside the loop use an if statement. The function can be written in about four lines of code.

I tried this:

def find(my_list, key):
    index = 0
    for element in my_list:
       if key == element:
            print(index)
            index  = 1

my_list = [36, 31, 79, 96, 36, 91, 77, 33, 19, 3, 34, 12, 70, 12, 54, 98, 86, 11, 17, 17]
find(my_list, 5)

But nothing really happened, no error, no result.

I've been struggling with this problem for while now, some help is really appreciated!

CodePudding user response:

The function you have written should work if it is properly indented:

my_list = [36, 31, 79, 96, 36, 91, 77, 33, 19, 3, 34, 12, 70, 12, 54, 98, 86, 11, 17, 17]


def find(my_list, key):
    index = 0
    for element in my_list:
        if key == element:
            print(index)
        index  = 1

find(my_list, 12)
>>> 11
>>> 13
find(my_list, 91) 
>>> 5
find(my_list, 80)
>>>

However, since they ask you to use a for loop with index variable and a range, I suggest the following:

def find(my_list, key):
    for i in range(len(my_list)):
        if my_list[i] == key:
            print('Found', key, 'in position', i)

find(my_list, 12)
>>> Found 12 in position 11
>>> Found 12 in position 13
find(my_list, 91)
>>> Found 91 in position 5
find(my_list, 80)
>>>

CodePudding user response:

enumerate():- function adds a counter as the key of the enumerate object.

Code:-

def find(my_list,num):
    lis=[]
    for index,value in enumerate(my_list):
        if value==num:
            lis.append(index)
    return lis if len(lis)>0 else "In list there is no number " str(num)


my_list = [36, 31, 79, 96, 36, 91, 77, 33, 19, 3, 34, 12, 70, 12, 54, 98, 86, 11, 17, 17]
# Testcase1
print(find(my_list, 12)) 
#Testcase2
print(find(my_list, 91)) 
#Testcase3
print(find(my_list, 80))

Output:-

[11, 13]
[5]
In list there is no number 80

# List Comprehension [One liner]:-

def find(my_list,num):
    return [index for index,value in enumerate(my_list) if value==num]
    
my_list = [36, 31, 79, 96, 36, 91, 77, 33, 19, 3, 34, 12, 70, 12, 54, 98, 86, 11, 17, 17]
# Testcase1
print(find(my_list, 12)) 
#Testcase2
print(find(my_list, 91)) 
#Testcase3
print(find(my_list, 80))

Output:-

[11, 13]
[5]
[]
  • Related