From 5e541727a4825a7d21eb1d91561f293b28972c80 Mon Sep 17 00:00:00 2001 From: TechWizard999 <119874392+TechWizard9999@users.noreply.github.com> Date: Wed, 4 Oct 2023 18:58:24 +0530 Subject: [PATCH] Create bNZkJm.py Signed-off-by: TechWizard999 <119874392+TechWizard9999@users.noreply.github.com> --- OP9pw8/bNZkJm.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 OP9pw8/bNZkJm.py diff --git a/OP9pw8/bNZkJm.py b/OP9pw8/bNZkJm.py new file mode 100644 index 00000000..406cd06e --- /dev/null +++ b/OP9pw8/bNZkJm.py @@ -0,0 +1,16 @@ +def insert_at_position(lst, position, element): + if position < 0: + print("Position cannot be negative") + return lst + elif position > len(lst): + print("Position out of range, appending element to the end of the list") + lst.append(element) + else: + lst.insert(position, element) + + return lst + +# Test +lst = [1, 2, 3, 4, 5] +inserted_lst = insert_at_position(lst, 2, 99) +print(inserted_lst)