just a heads up. My code probably is way off. I'm new and i'm trying my best to figure it out but i'm struggling
So far i have this:
def insert_value(my_list, value, insert_position):
new_list = []
for i in range(len(my_list)):
if i == insert_position:
new_list.append(value)
i += 1
return new_list
this is the code calling my function:
str_list3 = ['one','three','four', 'five', 'six']
new_list = list_function.insert_value(str_list3, 'two', 1)
print(new_list)
str_list4 = ['i', 't']
str_list4 = list_function.insert_value(str_list4, 'p', 0)
print(str_list4)
str_list4 = list_function.insert_value(str_list4, 's', -1)
print(str_list4)
str_list4 = list_function.insert_value(str_list4, 's', 7)
print(str_list4)
and:
num_list2 = [1, 3, 4, 5, 6]
num_list2 = list_function.insert_value(num_list2, 2, 1)
print(num_list2)
Am i far off in my solution? I really want to understand where i'm going wrong
Output is meant to be:
['one', 'two', 'three', 'four', 'five', 'six']
['p', 'i', 't']
['s', 'p', 'i', 't']
['s', 'p', 'i', 't', 's']
and:
[1, 2, 3, 4, 5, 6]]
my output is:
['two']
['p']
None
this is all i get
You are actually extremely close, you just need to add some boundary checks:
def insert_value(my_list, value, insert_position):
if insert_position < 0:
insert_position = 0
elif insert_position >= len(my_list):
insert_position = len(my_list)
new_list = []
for i in range(len(my_list)):
if i == insert_position:
new_list.append(value)
new_list.append(my_list[i])
if len(my_list) == insert_position:
new_list.append(value)
return new_list
str_list3 = ['one', 'three', 'four', 'five', 'six']
new_list = insert_value(str_list3, 'two', 1)
print(new_list)
str_list4 = ['i', 't']
str_list4 = insert_value(str_list4, 'p', 0)
print(str_list4)
str_list4 = insert_value(str_list4, 's', -1)
print(str_list4)
str_list4 = insert_value(str_list4, 's', 7)
print(str_list4)
num_list2 = [1, 3, 4, 5, 6]
num_list2 = insert_value(num_list2, 2, 1)
print(num_list2)
Output:
['one', 'two', 'three', 'four', 'five', 'six']
['p', 'i', 't']
['s', 'p', 'i', 't']
['s', 'p', 'i', 't', 's']
[1, 2, 3, 4, 5, 6]
You're pretty close except that you've neglected to insert the lines from the original list and you return inside the loop instead of after its done. Also, there is no need to increment i
. Its reassigned on the next loop with the next value from range
. Basically, range
takes the place of the increment.
def insert_value(my_list, value, insert_position):
new_list = []
for i in range(len(my_list)):
if i == insert_position:
new_list.append(value)
new_list.append(my_list[i])
return new_list
str_list3 = ['one','three','four', 'five', 'six']
new_list = insert_value(str_list3, 'two', 1)
print(new_list)
Instead of range
you could use enumerate
which counts but also gives you the lines iterated, so you don't have to index the array again.
def insert_value(my_list, value, insert_position):
new_list = []
for i,v in enumerate(my_list):
if i == insert_position:
new_list.append(value)
new_list.append(v)
return new_list
str_list3 = ['one','three','four', 'five', 'six']
new_list = insert_value(str_list3, 'two', 1)
print(new_list)
You already have a few response on this question. Here is another option to consider:
def insert_value(my_list, value, insert_position):
if insert_position < 0:
insert_position = (-insert_position) - 1
#if you want any negative value to result in position 0
#then your insert_position assignment will be
#if insert_position < 0: insert_position = 0
#the rest of the code will remain the same
my_list.insert(insert_position,value)
return my_list
str_list3 = ['one','three','four', 'five', 'six']
new_list = insert_value(str_list3, 'two', 1)
print(new_list)
str_list4 = ['i', 't']
str_list4 = insert_value(str_list4, 'p', 0)
print(str_list4)
str_list4 = insert_value(str_list4, 's', -1)
print(str_list4)
str_list4 = insert_value(str_list4, 's', 7)
print(str_list4)
num_list2 = [1, 3, 4, 5, 6]
num_list2 = insert_value(num_list2, 2, 1)
print(num_list2)
The output for these will be:
['one', 'two', 'three', 'four', 'five', 'six']
['p', 'i', 't']
['s', 'p', 'i', 't']
['s', 'p', 'i', 't', 's']
[1, 2, 3, 4, 5, 6]
#custom function for insert
insert_value =lambda a,value,idx: a[:idx] + [value] + a[idx:]
example
str_list3 = ['one','three','four', 'five', 'six']
new_list = insert_value(str_list3, 'two', 1)
print(new_list)
str_list4 = ['i', 't']
str_list4 = insert_value(str_list4, 'p', 0)
print(str_list4)
str_list4 = insert_value(str_list4, 's', -1)
print(str_list4)
str_list4 = insert_value(str_list4, 's', 7)
print(str_list4)
num_list2 = [1, 3, 4, 5, 6]
num_list2 = insert_value(num_list2, 2, 1)
print(num_list2)
The output for these will be:
['one', 'two', 'three', 'four', 'five', 'six']
['p', 'i', 't']
['s', 'p', 'i', 't']
['s', 'p', 'i', 't', 's']
[1, 2, 3, 4, 5, 6]
Another example but source list will change if need you can copy it
Syntax: lst[index:index] = [obj]
Here is without custom function
str_list = ['one','three','four', 'five', 'six']
str_list[1:1]=['two']
print(str_list)
str_list1=['i', 't']
str_list1[0:0]='p'
print(str_list1)
str_list1[-1:-1]=['s']
print(str_list1)
str_list1[7:7]=['s']
print(str_list1)
num_list2 = [1, 3, 4, 5, 6]
num_list2[1:1]=[2]
print(num_list2)
The output for these will be:
['one', 'two', 'three', 'four', 'five', 'six']
['p', 'i', 't']
['s', 'p', 'i', 't']
['s', 'p', 'i', 't', 's']
[1, 2, 3, 4, 5, 6]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.