Reputation: 43
I'm attempting to make a function that when called, takes a list, value, and insert position, and returns a copy of the list with the value inserted at the place specified by the insert position. For this task (due to request), I'm unable to use bulit in functions (other than range(), slice expressions, list methods (apart from append()) or string methods. Any help would be greatly appreciated
def insert_value(my_list, value, insert_position):
new_list = []
for i in my_list:
new_list.append(i)
if insert_position > length(my_list):
new_list.append(value)
return new_list
elif insert_position <= 0:
new_list.append(value[0])
return new_list
str_list3 = ['one','three','four', 'five', 'six']
new_list = list_function.insert_value(str_list3, 'two', 1)
print(new_list)
Upvotes: 0
Views: 1626
Reputation: 20216
def insert_value(my_list, value, insert_position):
new_list = []
if insert_position <= 0:
new_list.append(value)
for i in my_list:
new_list.append(i)
elif insert_position >= len(my_list):
for i in my_list:
new_list.append(i)
new_list.append(value)
else:
new_list = [None] * (len(my_list) + 1)
# replace : index = [i for i in range(len(new_list)) if i != insert_position]
index = []
for i in range(len(new_list)):
if i != insert_position:
index.append(i)
# end
new_list[insert_position] = value
for i in index:
# replace : new_list[i] = my_list[i if i < insert_position else i - 1]
if i < insert_position:
new_list[i] = my_list[i]
else:
new_list[i] = my_list[i - 1]
# end
return new_list
def insert_value(my_list, value, insert_position):
new_list = []
if insert_position <= 0:
new_list.append(value)
for i in my_list:
new_list.append(i)
elif insert_position >= len(my_list):
for i in my_list:
new_list.append(i)
new_list.append(value)
else:
new_list = [None] * (len(my_list) + 1)
index = [i for i in range(len(new_list)) if i != insert_position]
new_list[insert_position] = value
for i in index:
new_list[i] = my_list[i if i < insert_position else i - 1]
return new_list
Upvotes: 1
Reputation: 22953
This can be accomplished very simply using slice notation.
You known what your function needs to do. Insert a given value into a given position in a given list. Basically, you need to split the list into two parts. The first part contains all the elements before the given index. The second part contains all the elements after the given index. Once you have those two parts, you can simply "sandwich" the given value in between.
Here is visual demonstrating what I mean. Assume the index is equal to 1
:
first part value second part
| | |
[1]-------------2----------------[3][4][5]
| |
| |
| |
+ all elements *before* index 1 + all elements *after* index 1
Here is how the implementation would look like:
>>> def insert_value(lst, value, idx):
... # elements before idx
... front = lst[:idx]
... # elements after idx
... back = lst[idx:]
... # "sandwhiching" the value between the front and back parts
... return front + [value] + back
example:
>>> lst = [1, 3, 4, 5]
>>> insert_value(lst, 2, 1)
[1, 2, 3, 4, 5]
>>>
I'm not exactly sure if you have to, but it would be trivial to implement a check to see if the given index is in the bounds of the lists.
Upvotes: 2
Reputation: 86
do this :
def insert_value(my_list, value, insert_position):
new_list = []
for i in range(0, len(my_list)+1):
if i < insert_position:
new_list.append(my_list[i])
elif insert_position == i:
new_list.append(value)
else:
new_list.append(my_list[i-1])
return new_list
str_list3 = ['one','three','four', 'five', 'six']
new_list = insert_value(str_list3, 'two', 1)
print(new_list)
Upvotes: 0
Reputation: 108
You can do this, given that you're inserting a single value:
def insert_value(my_list, value, insert_position):
new_list = []
for i in range(0, insert_position)
new_list.append(my_list[i])
new_list.append(value)
for i in range(insert_position, len(my_list)+1)
new_list.append(my_list[i-1])
If you can use splicing and append, then
def insert_value(my_list, value, insert_position):
new_list = my_list[:insert_position]
new_list.append(value)
new_list = new_list+my_list[insert_position:]
Upvotes: 0