Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Python List Search Function
00:00
5 left

Python List Search Function

EasyPython

Problem

Write a function in Python which will search an element of a list.

Implement search_element(items, target) to return the zero-based index of the first occurrence of target in items. Return -1 if the target does not appear. The list may be empty and may contain duplicate values.

Examples:

  • items = [4, 9, 2, 7], target = 2 returns 2.
  • items = [5, 3, 5], target = 5 returns 0.

The function should accept a list and a single comparable target value, and return an integer.

Constraints

  • 0 <= len(items) <= 10^5
  • Elements in items and target support equality comparison
  • Return the first matching index

Function Signature

def search_element(items, target):
Interviewer

Your question is Python List Search Function. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.