简体   繁体   中英

How to remove an item from a pre-existing list using a for loop and through the use of append?

Essentially what I'm trying to do is remove values within a list when a condition has been met.

The conditions are based upon the position of the value in the list, which is what remove_position stands for in the function definition. The code below is what I have so far and I'm using Python 3.6.0. The list in this case is from an external python file which imports the function from another python and is:

str_list5 = ['w', 'i', 'n', 'g']
new_list = list_function.remove_value(str_list5, 2)
print(new_list)
new_list = list_function.remove_value(str_list5, -1)
print(new_list)
new_list = list_function.remove_value(str_list5, 10)
print(new_list)

What I'm trying to do use the remove_position value above in an arithmetic function that will delete the item that corresponds to the function's result.

def remove_value(my_list, remove_position):
    newlist = []
    count = 0

    for item in my_list:
        if remove_position < count:
            newlist.remove(item)

        if remove_position > count:
            newlist.remove(item)

    return newlist

The output that I'm looking for is this:

remove_value Test
['w', 'i', 'g']
['i', 'n', 'g']
['w', 'i', 'n']

You can do something like this:

my_list = ['w','i','n','g']

def remove_value(my_list, position):
    if position >= len(my_list):
        return my_list[:-1]
    elif position < 0:
        return my_list[1:]
    else:
        return my_list[:position] + my_list[position+1:]


# Test
remove_value(my_list, 2)
remove_value(my_list, -1)
remove_value(my_list, 10)

Output:

['w', 'i', 'g']
['i', 'n', 'g']
['w', 'i', 'n']

First of all : list objects are mutable and when you change them inside of a function – the original changes too, so if you use list.remove method in remove_value your str_list5 loses element as well.

I advise you to create new object instead of mutating the old one. We can write this

def remove_value(my_list, remove_position):
    result = []
    remove_position = min(max(remove_position, 0), len(my_list) - 1)
    for position, element in enumerate(my_list):
        if position == remove_position:
            continue
        result.append(element)
    return result

or using list comprehension

def remove_value(my_list, remove_position):
    remove_position = min(max(remove_position, 0), len(my_list) - 1)
    return [element
            for position, element in enumerate(my_list)
            if position != remove_position]

or following @ChihebNexus suggestion using list slices

def remove_value(my_list, remove_position):
    remove_position = min(max(remove_position, 0), len(my_list) - 1)
    return my_list[0: remove_position] + my_list[remove_position + 1:]

So which one to choose? The last version looks more elegant to me.

Tests

str_list5 = ['w', 'i', 'n', 'g']
new_list = remove_value(str_list5, 2)
print(new_list)
new_list = remove_value(str_list5, -1)
print(new_list)
new_list = remove_value(str_list5, 10)
print(new_list)

gives us

['w', 'i', 'g']
['i', 'n', 'g']
['w', 'i', 'n']

Try this

    def remove_value(my_list, remove_position):
    my_list.pop(remove_position)

    return my_list

str_list5 = ['w','i','n','g']
new_list = remove_value(str_list5, 2)
print(new_list)

list comprehension is a good choice

new_list = [x for x in old_list if your_condition]

specifically, you may want this

def remove_value(my_list, remove_position):
    return [my_list[i] for i in range(len(my_list)) if i != remove_position]

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM