1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Python: Removing an Item From a List, But Returns a New List

Discussão em 'Python' iniciado por Stack, Junho 26, 2025.

  1. Stack

    Stack Membro Participativo

    Is there anyway to remove an item from a list in such a way that I return a new list without the removed element?

    For instance, if I have a list ['a','b','c','d','e'] (called my_list), then I would like a method that does the following my_list.newRemove('a') = ['b','c','d','e'].

    I want to do this in such a way that I could send the element to be removed and the new list as parameters of a function:

    func(element, my_list.newRemove(element))


    for example:

    func('a', list.newRemove('a'))


    which would pass 'a' AND ['b','c','d','e']

    I know I can do this by just passing the element and the entire list to the function, and then creating a new subList in the function without the passed element. However, I feel like it would look better to do it the way I'm asking.

    Is this just a fundamentally un-pythonic way of doing this? I learned to program with functional languages, and old habits die hard.


    Edit: mistakenly wrote set rather than list

    Edit 2: Here is an example of what I ultimately want to do:

    myList = ['a','b','c','d']

    for element in myList:
    function(element, myList.newRemove(element)


    Where function does a calculation between the element and the rest of the elements in the list.

    Continue reading...

Compartilhe esta Página