Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove tuples from a list if their first items are equal to any of the given strings

Tags:

python

I have a list as below:

device = [('nvme2n1',), 
          ('nvme1n1', '/local'), 
          ('nvme0n1',), 
          ('nvme0n1p1', '/'),
          ('nvme0n1p128',), 
          ('nvme3n1',)]

I want to delete few tuples from this list containing nvme1n1 or nvme0n1p1 or nvme0n1p128 or nvme0n1.

so the final list will have

final_device = [('nvme2n1',),('nvme3n1',)]

tried as below but didn't work & got error "AttributeError: 'tuple' object has no attribute 'startswith'"

for word in devices[:]: 
    if word.startswith("nvme0n1","nvme0n1p1","nvme0n1p128"): 
        devices.remove(word)

Can anyone help with this?

like image 961
dbNovice Avatar asked Nov 07 '20 06:11

dbNovice


People also ask

How do you remove a tuple from a list?

Use the del statement to remove a tuple from a list of tuples, e.g. del list_of_tuples[0] . The del statement can be used to remove a tuple from a list by its index, and can also be used to remove slices from a list of tuples.

How do you remove the first value from a tuple?

To remove the first element from a tuple in Python, slice the original tuple from the second element (element at index 1) to the end of the tuple. This will result in a new tuple with the first element removed.

How do you remove a tuple?

To explicitly remove an entire tuple, just use the del statement.

How do you remove duplicates from a tuple list?

Remove duplicates from a tuple using a set You can use a set to remove duplicates from a tuple in Python. First, create a set of unique elements from the tuple using the set() function and then use the tuple() function to create a new tuple from the resulting set.


Video Answer


4 Answers

devices = [('nvme2n1',), ('nvme1n1', '/local'),
           ('nvme0n1',), ('nvme0n1p1', '/'), 
           ('nvme0n1p128',), ('nvme3n1',)]
devices = [device for device in devices if device[0] not in 
           (("nvme0n1", "nvme0n1p1", "nvme0n1p128", "nvme0n1"))]
print(devices)

output

[('nvme2n1',), ('nvme1n1', '/local'), ('nvme3n1',)]

@JamesTollefson in their answer address the particular problem in your code and how to remedy it. This is just different and in my opinion better/cleaner way to achieve what you want.

like image 121
buran Avatar answered Oct 26 '22 07:10

buran


You can even use a list comprehension to make it simpler. As James mentioned in his answer, you need to do word[0] as word is a tuple and not a string.

startswith can take a tuple to check with.

[dev for dev in devices if not dev[0].startswith(("nvme0n1","nvme0n1p1","nvme0n1p128"))]

But if you're looking for exact matches, you can do,

[dev for dev in devices if dev[0] not in ("nvme0n1","nvme0n1p1","nvme0n1p128")]
like image 31
Durga Swaroop Avatar answered Oct 26 '22 05:10

Durga Swaroop


The words you are iterating through in the devices list are tuples. You need to access that part of that tuple you are interested in as follows:

for word in devices[:]: 
    if word[0] in ["nvme0n1","nvme0n1p1","nvme0n1p128"]: 
        devices.remove(word)
like image 21
James Tollefson Avatar answered Oct 26 '22 05:10

James Tollefson


@JamesTollefson's answer is nice and gets to the heart of the matter.

As a side note, just wanted to add that when dealing with lists and dicts, you can make your code more concise and elegant using list comprehensions:

devices = [d in devices if d[0] not in {"nvme0n1", "nvme0n1p1", "nvme0n1p128", "nvme0n1"}]

like image 2
Asker Avatar answered Oct 26 '22 05:10

Asker