Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert a value after another value in a list

Tags:

python

list

I would like a function called insert_after which will take a list and two values (search_value and value).

The functionality should then be to insert value after the first occurrence of search_value

If search_value is not in the list, then add it to the end. I would like to use a try...except statement to do this.

For example, if the list is:

myList = [4,2,6,7,8,1], 

then the function call:

insert_after(myList, 7, 5)

should return:

[4,2,6,7,5,8,1]

I've attempted it, but my value keeps getting inserted at the end of the list even though I specify the index.

def insert_after(list1, search_value, value):
    try:
        for i in list1:
            if i == search_value:
                list1.insert(search_value+1,value)
            else:
                list1.insert(len(list1)+1,value)
    except:
        list1.insert(len(list1)+1,value)
like image 456
verdy Avatar asked Mar 24 '18 12:03

verdy


2 Answers

You need to first find the index of search_value in the list, which can be done with the .index method. Then, you can use the .insert method to insert the value at the position after that (the index +1).

However, we need to consider the case where search_value is not in lst. For this, we simply use a try...except to catach the ValueError for when the .index fails. And, in this case, we want to either append to lst, or .insert at the end; either works.

def insert_after(lst, search_value, value):
    try:
        lst.insert(lst.index(search_value)+1, value)
    except ValueError:
        lst.append(search_value)
        #or: lst.insert(len(lst)-1, value)

and a test:

>>> l = [4, 2, 6, 7, 8, 1]
>>> insert_after(l, 7, 5)
>>> l
[4, 2, 6, 7, 5, 8, 1]

why didn't your method work?

If we look closely at your main insertion line:

list1.insert(search_value+1,value)

we can see that your logic is slightly off. The .insert method takes an index and a value. Here, you are passing search_value+1 as the index even though this is really just the value.

So hopefully you can see from my code, that using the .index method is the right way to go since it gives us the index of that value - allowing us to use .insert correctly.

what if you don't want to use .index?

So, yes, you could use a for-loop, but instead of iterating over the terms as you are, you really want to be iterating over the values and the indexes. This can be achieved using enumerate().

So, I will let you put this in a function by yourself since it is likely you will just end up using the .index method, but the basic idea would be something along the lines of:

for i, e in enumerate(lst):
    if e == search_value:
        lst.insert(i+1, value)
like image 107
Joe Iddon Avatar answered Sep 24 '22 12:09

Joe Iddon


syntax of insert => list1.insert(index,element);

but here you specify search_value.and also you can use index function to get the index of a value in list.

the function look like this.

def insert_after(list1, search_value, value):
try:
    index = list1.index(search_value);
    list1.insert(index+1,value);
except:
    list1.insert(len(list1)+1,value)

when the value not present in the list it will raise ValueError.

like image 29
hema sundar Ginni Avatar answered Sep 22 '22 12:09

hema sundar Ginni