verdy
verdy

Reputation: 89

Insert a value after another value in a 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)

Upvotes: 5

Views: 1425

Answers (5)

Rahul
Rahul

Reputation: 11520

To achieve that you'll need to get the index value for the point you want to insert your value.

def insert_after(myList, i, j):
    try:
        myList.insert(myList.index(i)+1, j)
    except ValueError:
        myList.append(j)

You can read a bit more on lists here; https://docs.python.org/2/tutorial/datastructures.html

Upvotes: 0

Olivier Melançon
Olivier Melançon

Reputation: 22294

Yet another way is to find the index of insertion with a generator. This allows to give a default value to hide the exception handling of list.index.

Here is a solution that creates a new list.

def insert_after(list1, search_value, value):
    i = next((i + 1 for i in range(len(list1)) if list1[i] == search_value), len(list1))
    return [*list1[:i], value, *list1[i:]]

insert_after([1, 2, 3], 2, 'foo') # [1, 2, 'foo', 3]
insert_after([1, 2, 3], 10, 'bar') # [1, 2, 3, 'bar']

And here is a solution that mutates your existing list.

def insert_after(list1, search_value, value):
    i = next((i + 1 for i in range(len(list1)) if list1[i] == search_value), len(list1))
    list1.insert(i, value)

list1 = [1, 2, 3]
insert_after(list1, 2, 'foo')

list1 # [1, 2, 'foo', 3]

Upvotes: 0

Joe Iddon
Joe Iddon

Reputation: 20414

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)

Upvotes: 5

hema sundar Ginni
hema sundar Ginni

Reputation: 323

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.

Upvotes: 1

Haifeng Zhang
Haifeng Zhang

Reputation: 31885

def get_index(mylist, search_value):
    """return index number; returns -1 if item not found"""
    try:
        return mylist.index(search_value)
    except ValueError as ve:
        # ValueError would be thrown if item not in list
        return -1  # returns -1 if item not in list

def insert_after(list1, search_value, value):
    index = get_index(list1, search_value)
    if index != -1:
        """inserts after index"""
        list1.insert(index + 1, value)
    else:
        """appends to the end of list"""
        list1.append(value)

Upvotes: 0

Related Questions