Python Forum
To find the index of the first occurrence of the key in the provided list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
To find the index of the first occurrence of the key in the provided list
#1
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
Reply
#2
line 7:
print(qlist[dtlv])
Reply
#3
@Larz60+, that makes the code display 7, not 4
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#4
your right!
Reply
#5
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to fix list index out of range longmen 26 5,888 Apr-27-2022, 05:46 PM
Last Post: deanhystad
  list index out of range OliverG 3 2,326 Sep-03-2021, 12:04 AM
Last Post: itsmycode
  Index List a04j 2 2,910 Jul-10-2021, 01:14 PM
Last Post: BashBedlam
  List index out of range when turning CSV into dict ranbarr 15 6,402 May-12-2021, 10:38 AM
Last Post: ranbarr
  How to find difference between elements in a list? Using beginner Basics only. Anklebiter 8 4,316 Nov-19-2020, 07:43 PM
Last Post: Anklebiter
  List vs index: Frederico_Caldas 5 3,578 Jul-03-2020, 10:55 AM
Last Post: DeaD_EyE
  How do you find the length of an element of a list? pav1983 13 4,844 Jun-13-2020, 12:06 AM
Last Post: pav1983
  Find first letter from a list DariusCG 3 2,499 Nov-27-2019, 11:08 PM
Last Post: ichabod801
  Generating a dict from provided randomizer beLIEve 0 1,511 Oct-11-2019, 09:25 AM
Last Post: beLIEve
  list index out of range mcgrim 2 2,901 May-25-2019, 07:44 PM
Last Post: mcgrim

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020