Python Forum

Full Version: To find the index of the first occurrence of the key in the provided list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All,

Please could someone help with the error I am getting with my code

Description
The index of the first occurrence of the key in the provided list, if it is present, and -1 otherwise.

Output:
Input [1, 2, 3, 4, 5, 6] 10 Solution output -1 Expected output -1 Test Case Passed. Input [1, 2, 4, 2, 3, 2, 3, 3, 6, 7] 3 Solution output -1 Expected output 4 Test Case Failed
Sorry posted wrong code and error!!!!also just to let me know what changes to be done in the linked code to get the desired output.

#input have been taken for you here
import ast
input_str = input()
qlist = ast.literal_eval(input_str)
value=input()
dtlv= qlist.index(value) if (value in qlist) else -1
print(dtlv)
    
I'm really new to python can anyone help me to correct the code
line 7:
print(qlist[dtlv])
@Larz60+, that makes the code display 7, not 4
your right!
Hi All,

Please could someone help with the error I am getting with my code

Description
Find the intersection of two sorted lists

Output:
Input [[1, 2, 3, 5, 9], [3, 6, 9, 12]] Solution output 3 9 Expected output [3, 9] Testcase failed! The solution's output doesn't match the expected output. Input [[1, 1, 2, 4, 8, 8, 8, 9], [1, 2, 6, 7, 8, 8, 8]] Solution output 1 2 8 8 8 Expected output [1, 2, 8, 8, 8] Testcase failed! The solution's output doesn't match the expected output.
Sorry posted wrong code and error!!!!also just to let me know what changes to be done in the linked code to get the desired output.

# Reading the input
import ast 
input_lists = ast.literal_eval(input())

# Write your code here
# Python program to find intersection of 
# two sorted arrays 
# Function prints Intersection of arr1[] and arr2[] 
# m is the number of elements in arr1[] 
# n is the number of elements in arr2[] 
def printIntersection(arr1, arr2, m, n): 
    i,j = 0,0
    while i < m and j < n: 
        if arr1[i] < arr2[j]: 
            i += 1
        elif arr2[j] < arr1[i]: 
            j+= 1
        else:
            print(arr2[j])
            j += 1
            i += 1

#def printArray(arr2): 
#    j=0
#    print(arr2[j])  
# Driver program to test above function 

arr1 = input_lists[0]
arr2 = input_lists[1]

m = len(arr1) 
n = len(arr2) 

printIntersection(arr1, arr2, m, n) 
#printArray(arr2) 
I'm really new to python can anyone help me to correct the code