## Python removing tuples from list that satisfy given conditions

I have a list of tuples and i want to remove tuples so that there is only one tuple in the list that has a given length and sum.

That's a bad explanation so for example:

[(0,1,2), (0,2,1), (0,0,1)]

remove (0,1,2) or (0,2,1)

I want to be able to iterate though the list and remove any tuples that satisfy the following conditions:

len(tuple1) == len(tuple2) and sum(tuple1) == sum(tuple2)

but keep either tuple1 or tuple2 in the list.

I tried:

for t1 in list: for t2 in list: if len(t1) == len(t2) and sum(t1) == sum(t2): list.remove(t1)

but im pretty sure this removes all tuples and the console crashed.

In essence this is a "uniqness filter", but where we specify a function `f`

, and only if that `f(x)`

occurs a second time, we filter that element out.

We can implement such uniqness filter, given `f(x)`

produces *hashable* values, with:

def uniq(iterable, key=lambda x: x): seen = set() for item in iterable: u = key(item) if u not in seen: yield item seen.add(u)

Then we can use this filter as:

result = list(uniq(data, lambda x: (len(x), sum(x))))

for example:

>>> list(uniq(data, lambda x: (len(x), sum(x)))) [(0, 1, 2), (0, 0, 1)]

Here we will always retain the first occurrence of the "duplicates".

**Python,** Given a list of a tuple, the task is to remove all the tuples from list, if it's greater than n (say Python code to demonstrate removing tuples for condition met. Is Python call by reference or call by value Python | Remove tuples from list of tuples if greater than n Given a list of a tuple, the task is to remove all the tuples from list, if it’s greater than n (say 100). Let’s discuss a few methods for the same.

Let me offer a slightly different solution. Note that this is not something I'd use for a one-off script, but for a real project. Because your `[(0, 0, 1)]`

actually represents something logical/physical.

`set(..)`

removes duplicates. How about we use that? The only thing to keep it mind is that the hash value and equality of the elements need to be modified.

class Converted(object): def __init__(self, tup): self.tup = tup self.transformed = len(tup), sum(tup) def __eq__(self, other): return self.transformed == other.transformed def __hash__(self): return hash(self.transformed) inp = [(0,1,2), (0,2,1), (0,0,1)] out = [x.tup for x in set(map(Converted, inp))] print(out) # [(0, 0, 1), (0, 1, 2)]

**remove elements in one list according to the conditions on ,** I should say that this works in python 3.6. The following code creates a list of tuples from the elements of 'lstA' and 'lstB' excluding the tuple that Python | Remove duplicate tuples from list of tuples Given a list of tuples, Write a Python program to remove all the duplicated tuples from the given list. Examples:

You can also use `groupby`

to group elements by `sum`

and `len`

and fetch 1 element from each group to create a new list:

from itertools import groupby def _key(t): return (len(t), sum(t)) data = [(0, 1, 2), (0, 2, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0), (3, 0, 0, 0)] result = [] for k, g in groupby(sorted(data, key=_key), key=_key): result.append(next(g)) print(result) # [(0, 0, 1), (0, 1, 2), (3, 0, 0, 0)]

**5. Data Structures,** list.pop([i]): Remove the item at the given position in the list, and return it. or to create a subsequence of those elements that satisfy a certain condition. If the expression would evaluate to a tuple, it must be parenthesized. I am having trouble with removing a user inputted name from the list along with the students marks. This is the code i have so far studentlist=[] a=1 while a!=0: print (""" 1.

The complexity of your problem comes mainly from the fact that you have two independent filters you want to implement. A good way to go about filtering on data with this sort of requirement is to use `groupby`

. However, before you can do that you need to sort first. Since you normally sort over one key, you'll need to sort twice before you can group:

from itertools import groupby def lensumFilter(data): return [next(g) for _, g in groupby(sorted(sorted(data, key = len), key = sum), key = lambda x: (len(x), sum(x)))] >>> print(lensumFilter( [(0, 1, 2), (0, 2, 1), (0, 0, 1)] ) [(0, 0, 1), (0, 2, 1)] >>> print(lensumFilter( [(0, 1, 2), (0, 2, 1), (0, 0, 0, 3), (0, 0, 1)] ) [(0, 0, 1), (0, 2, 1), (0, 0, 0, 3)] >>> print(lensumFilter( [(0, 1, 2), (0, 2, 2), (0, 4), (0, 0, 0, 5), (0, 0, 3)] ) [(0, 1, 2), (0, 4), (0, 2, 2), (0, 0, 0, 5)]

Note that if you change how the sorts work, you change how the output will look. For instance, I sorted on length and then sum so my results are in order with respect to sum (smallest sum first) and then in order with respect to length (fewest number of elements first) within sum-groupings. That's why `(0, 1, 2)`

comes before `(0, 4)`

but `(0, 4)`

comes before `(0, 2, 2)`

.

**5. Data Structures,** Remove the item at the given position in the list, and return it. a subsequence of those elements that satisfy a certain condition. If the expression is a tuple (e.g. the (x, y) in the previous example), it must be parenthesized. You can convert your list y to a set so that you remove duplicates import itertools x = [1,1,1,2,2,2,3,3,3,4,4,5] y = [] for x in itertools.permutations(x,3): y.append(x) print(set(y)) And voila, you are done.

If you want to do something concise and more pythonic, you could use the function filter. It will keep all the elements that are matching your requirements (here sum being not equal when same length):

tup_remove = (0,2,1) list(filter(lambda current_tup: not (sum(tup_remove) == sum(current_tup) and len(tup_remove) == len(current_tup))

For better readability and extensibility, I would encourage you to use a function:

def not_same_sum_len_tuple(tup_to_check, current_tuple): """Return True when not same sum AND same length""" same_sum = sum(tup_to_check) == sum(current_tuple) # Check the sum same_len = len(tup_remove) == len(current_tuple) # Check the length return not (same_sum and same_len) tup_remove = (0,2,1) list(filter(lambda current_tup: not_same_sum_len_tuple(tup_remove, current_tup), tup_list))

**Delete elements from a Numpy Array by value or conditions in Python,** Remove all occurrences of an element with given value from numpy array to select the elements only which satisfies the given conditions. Basically I want to remove every tuple in my list that contains a 1 and for some weird reason, it only removes 1/2 tuples (I did this with a much bigger list, from 1111 to 6666 and it was removing 1111 and 1113 etc but not 1112 and 1114 etc).

Python | Creating a Pandas dataframe column based on a given condition; Python | Delete rows/columns from DataFrame using Pandas.drop() Selecting rows in pandas DataFrame based on conditions; Return the Index label if some condition is satisfied over a column in Pandas Dataframe; Create a new column in Pandas DataFrame based on the existing columns

In Python, list's methods clear(), pop(), and remove() are used to remove items (elements) from a list. It is also possible to delete items using del statement by specifying a position or range with an index or slice.Remove all items: clear() Remove an item by index and get its value: pop() Remove a

Python | Remove empty tuples from a list In this article, we will see how can we remove an empty tuple from a given list of tuples. We will find various ways, in which we can perform this task of removing tuples using various methods and ways in Python.

##### Comments

- It is also very unsafe to
*iterate*and*modify*a collection at the same time, so this is not the way to go. - what if tuple [a,b,c,d,e,a'] - should only a or a' stay in or do you only test neighboring ones?
- I like this but it misses the length requirement.
- @mVChr I missed that. Fixed :)
- Python
`for`

has`else`

clause. - @NizamMohamed I always forget about that, fixed in edit, thanks!