I have a list of tuples and I want to filter out the all elements where the second value in the tuple is not equal to 7.
I do:
valuesAsList.filter(x=>x._2 != 7)
Can I use wildcard notation to make this even shorter?
Thanks.
Python has a built-in function called filter() that allows you to filter a list (or a tuple) in a more beautiful way. The filter() function iterates over the elements of the list and applies the fn() function to each element. It returns an iterator for the elements where the fn() returns True .
filter() method is a very useful method of Python. One or more data values can be filtered from any string or list or dictionary in Python by using filter() method. It filters data based on any particular condition. It stores data when the condition returns true and discard data when returns false.
Select a cell in the data table. On the Data tab of the Ribbon, in the Sort & Filter group, click Advanced, to open the Advanced Filter dialog box. For Action, select Filter the list, in-place.
You can select attributes of a class using the dot notation. Suppose arr is an array of ProjectFile objects. Now you filter for SomeCocoapod using. NB: This returns a filter object, which is a generator.
You can
valuesAsList.filter(_._2 != 7)
But I doubt it should be preferred over your example or this (think readability):
valuesAsList.filter {case (_, v) => v != 7}
Fairly straight forward, with no real advantage IMHO:
valuesAsList.filter(_._2 != 7)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With