Insert a value after another value in the list

I need a function called insert_afterthat will take a list and two values ​​( search_valueand value).

Then the functionality should be to insert valueafter the first appearancesearch_value

If search_valuenot listed, add it to the end. I would like to use the instructions try...exceptfor this.

For example, if the list:

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

then function call:

insert_after(myList, 7, 5)

should return:

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

I tried to do this, but my value continues to be inserted at the end of the list, although I am specifying an 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)
+4
source share
5 answers

search_value , .index. .insert value ( +1).

, search_value lst. try...except ValueError, .index. lst, .insert; .

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)

:

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

?

:

list1.insert(search_value+1,value)

, . .insert . search_value+1 , .

, , , .index - , , .insert.

, .index?

, , for-loop, , , . enumerate().

, , , .index, - :

for i, e in enumerate(lst):
    if e == search_value:
        lst.insert(i+1, value)
+5

insert = > list1.insert(, );

search_value.and , .

.

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)

, , ValueError.

+1
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)
0

To do this, you need to get the index value for the point where 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)

Here you can read a little more; https://docs.python.org/2/tutorial/datastructures.html

0
source

Another way is to find the insert index with the generator. This allows you to specify a default value to hide exception handling 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 the 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]
0
source

Source: https://habr.com/ru/post/1695277/


All Articles