# Python program to find Tuples with positive elements in List of tuples

• Last Updated : 30 Jan, 2023

Given a list of tuples. The task is to get all the tuples that have all positive elements.

Examples:

```Input : test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)]
Output : [(4, 5, 9)]
Explanation : Extracted tuples with all positive elements.```
```Input : test_list = [(-4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)]
Output : []
Explanation : No tuple with all positive elements. ```

Method #1 : Using list comprehension + all()

In this, all() is used to check for all the tuples, list comprehension helps in the iteration of tuples.

## Python3

 `# Python3 code to demonstrate working of` `# Positive Tuples in List` `# Using list comprehension + all()`   `# initializing list` `test_list ``=` `[(``4``, ``5``, ``9``), (``-``3``, ``2``, ``3``), (``-``3``, ``5``, ``6``), (``4``, ``6``)]`   `# printing original list` `print``(``"The original list is : "` `+` `str``(test_list))`   `# all() to check each element` `res ``=` `[sub ``for` `sub ``in` `test_list ``if` `all``(ele >``=` `0` `for` `ele ``in` `sub)]`   `# printing result` `print``(``"Positive elements Tuples : "` `+` `str``(res))`

Output

```The original list is : [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)]
Positive elements Tuples : [(4, 5, 9), (4, 6)]
```

Method #2 : Using filter() + lambda + all()

In this, the task of filtration is performed using filter() and lambda function.

## Python3

 `# Python3 code to demonstrate working of` `# Positive Tuples in List` `# Using filter() + lambda + all()`   `# initializing list` `test_list ``=` `[(``4``, ``5``, ``9``), (``-``3``, ``2``, ``3``), (``-``3``, ``5``, ``6``), (``4``, ``6``)]`   `# printing original list` `print``(``"The original list is : "` `+` `str``(test_list))`   `# all() to check each element` `res ``=` `list``(``filter``(``lambda` `sub: ``all``(ele >``=` `0` `for` `ele ``in` `sub), test_list))`   `# printing result` `print``(``"Positive elements Tuples : "` `+` `str``(res))`

Output

```The original list is : [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)]
Positive elements Tuples : [(4, 5, 9), (4, 6)]
```

Method #3 : Using find(),map(),list() and join()

Convert each tuple element to string and then convert that tuple to a list.After that join elements of list using space.Now check if that joined list(which is a string) contains – sign in it.If – sign is found then tuple contains negative elements.Ignore such tuples and add the other tuples to output list

## Python3

 `# Python3 code to demonstrate working of` `# Positive Tuples in List`   `# initializing list` `test_list ``=` `[(``4``, ``5``, ``9``), (``-``3``, ``2``, ``3``), (``-``3``, ``5``, ``6``), (``4``, ``6``)]`   `# printing original list` `print``(``"The original list is : "` `+` `str``(test_list))`   `res``=``[]` `for` `i ``in` `test_list:` `    ``x``=``list``(``map``(``str``,i))` `    ``a``=``" "``.join(x)` `    ``if``(a.find(``"-"``)``=``=``-``1``):` `        ``res.append(i)`   `# printing result` `print``(``"Positive elements Tuples : "` `+` `str``(res))`

Output

```The original list is : [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)]
Positive elements Tuples : [(4, 5, 9), (4, 6)]
```

Method #4 : Using list(),map(),join() and startswith() methods

## Python3

 `# Python3 code to demonstrate working of` `# Positive Tuples in List`   `# initializing list` `test_list ``=` `[(``4``, ``5``, ``9``), (``-``3``, ``2``, ``3``), (``-``3``, ``5``, ``6``), (``4``, ``6``)]`   `# printing original list` `print``(``"The original list is : "` `+` `str``(test_list))`   `res ``=` `[]` `for` `i ``in` `test_list:` `    ``x ``=` `sorted``(i)` `    ``x ``=` `list``(``map``(``str``, x))` `    ``b ``=` `"".join(x)` `    ``if``(``not` `b.startswith(``"-"``)):` `        ``res.append(i)`   `# printing result` `print``(``"Positive elements Tuples : "` `+` `str``(res))`

Output

```The original list is : [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)]
Positive elements Tuples : [(4, 5, 9), (4, 6)]
```

Method #5 :  By defining a function and using len() method

## Python3

 `# Python3 code to demonstrate working of` `# Positive Tuples in List`   `# initializing list` `test_list ``=` `[(``4``, ``5``, ``9``), (``-``3``, ``2``, ``3``), (``-``3``, ``5``, ``6``), (``4``, ``6``)]`   `# printing original list` `print``(``"The original list is : "` `+` `str``(test_list))` `res ``=` `[]`     `def` `fun(x):` `    ``c ``=` `0` `    ``for` `i ``in` `x:` `        ``if``(i > ``0``):` `            ``c ``+``=` `1` `    ``if``(c ``=``=` `len``(x)):` `        ``return` `True` `    ``return` `False`     `for` `i ``in` `test_list:` `    ``if``(fun(i)):` `        ``res.append(i)`   `# printing result` `print``(``"Positive elements Tuples : "` `+` `str``(res))`

Output

```The original list is : [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)]
Positive elements Tuples : [(4, 5, 9), (4, 6)]
```

Method #6: Using list comprehension + not any()

In this, not any() is used to check for all the tuples, list comprehension helps in the iteration of tuples.

## Python3

 `# Python3 code to demonstrate working of` `# Positive Tuples in List` `# Using list comprehension + all()`   `# initializing list` `test_list ``=` `[(``4``, ``5``, ``9``), (``-``3``, ``2``, ``3``), (``-``3``, ``5``, ``6``), (``4``, ``6``)]`   `# printing original list` `print``(``"The original list is : "` `+` `str``(test_list))`   `# not any() to check each element` `res ``=` `[sub ``for` `sub ``in` `test_list ``if` `not` `any` `(ele < ``0` `for` `ele ``in` `sub)]`   `# printing result` `print``(``"Positive elements Tuples : "` `+` `str``(res))`

Output

```The original list is : [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)]
Positive elements Tuples : [(4, 5, 9), (4, 6)]
```

Time Complexity:O(n)
Auxiliary Space :O(n)

Method #7: Without using built-in function

## Python3

 `test_list ``=` `[(``4``, ``5``, ``9``), (``-``3``, ``2``, ``3``), (``-``3``, ``5``, ``6``), (``4``, ``6``)]` `result ``=` `[]` `# printing original list` `print``(``"The original list is : "` `+` `str``(test_list))` `for` `tup ``in` `test_list:` `    ``positive ``=` `True` `    ``for` `ele ``in` `tup:` `        ``if` `ele < ``0``:` `            ``positive ``=` `False` `            ``break` `    ``if` `positive:` `        ``result.append(tup)` `# printing result` `print``(``"Positive elements Tuples : "` `+` `str``(result))`   `#This code contributed by Vinay Pinjala.`

Output

```The original list is : [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, 6)]
Positive elements Tuples : [(4, 5, 9), (4, 6)]
```

Time Complexity:O(n)
Auxiliary Space :O(n)

My Personal Notes arrow_drop_up
Related Articles