How to remove a dropdown from within a list

Asked

Viewed 792 times

0

Good Afternoon, I have a problem with a job that is: I have a function that needs lists to organize various elements according to predefined characteristics. My question is how to remove a dropdown from within a list as presented in the example:

[('2019-02-20', '12:30', 'iCageDoree', 'Pedro Ruivo, lisbon, (heating; doors; windows), 5*, 75, 2019-03-22, 09:15, 3523.0')]

To look like this:

['2019-02-20', '12:30', 'iCageDoree', 'Pedro Ruivo, lisbon, (heating; doors; windows), 5*, 75, 2019-03-22, 09:15, 3523.0']
  • Well, what happens here is that what you’re using to enter the data into that list, is sending the data as a dropdown. Instead of getting around the problem with more lines of code, I suggest you fix how to send the data to the list so that it is not inserted as a single file. Anyway the solution presented above works perfectly.

1 answer

0


There are two ways to remove an item from within a list:

  • Through command del

  • By the method remove

Commando del

Let’s say we have the following list: listadetuplas = [(1, 2, 3), ('a', 'b', 'c')]. To remove an item, regardless of what it is, use the del as follows:

del listadetuplas[1]

That is, we are removing the item/tuple present in index/position 1 from the list, no matter which item/tuple is in that position - if the given position does not "exist", an error will be returned. That way, our list will look like this: [(1, 2, 3)].

Method remove

Considering the same example of list and its initial value, to remove a specific item, we can use the method remove as follows:

listadetuplas.remove((1, 2, 3))

That is, we are specifically removing the tuple (1, 2, 3) from within the list, no matter what position/index it is in now. That way, our list would look like this: [('a', 'b', 'c')].

[Edited]

So just take the class list and convert the tuple into a list, like this:

list(('2019-02-20', '12:30', 'iCageDoree', 'Pedro Ruivo, lisbon, (heating; doors; windows), 5*, 75, 2019-03-22, 09:15, 3523.0'))

or

list([('2019-02-20', '12:30', 'iCageDoree', 'Pedro Ruivo, lisbon, (heating; doors; windows), 5*, 75, 2019-03-22, 09:15, 3523.0')][0])

I hope I’ve helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.