Change language

# Python | Count the sublists containing the given item in the list

| |

Examples :

`  Input:  lst = [1, 3, 5], [1, 3, 5, 7], [1 , 3, 5, 7, 9]] x = 1  Output:  3  Input:  lst = ([’a’], [’ a’, ’c’, ’b’], [’ d’]) x = ’a’  Output:  2 `

Approach # 1: Naive Approach

Count the number of lists containing x . Initialize counter to 0, then run a for loop and check if x exists in each list or not. If so, increase the counter .

 ` # Python3 Counting program ` ` # a list containing a specific element ` ` # in a list of lists ` ` `  ` def ` ` countList (lst, x): ` ` count ` ` = ` ` 0 ` ` for ` ` i ` ` in ` ` range ` ` (` ` len ` ` (lst)): ` ` if ` ` x in lst [i]: `` count + = 1   return count    Driver code lst = ( [ ’a’ ], [ ’ a’ , ’c’ , ’ b’ ], [ ’d’ ])  x = ’a’   print (countList (lst, x)) `

Exit :

` 2 `

Approach # 2: Understanding the list (Alternative for the naive)
A simple one-line list comprehension can also help by simply converting the above naive approach to a one-line loop.

` `

` # Python3 Program for counting numbers # a list containing a specific element # in a list of lists   def countList ( lst, x):   return sum (x in item for item in lst)    Driver code lst = ([ ’a’ ], [ ’a’ , ’ c’ , ’b’ ], [ ’ d’ ])  x = ’a’ print (countList (lst, x )) `

` ` Exit:

` 2 `

Approach # 3:  Using ` chain.from_iterable () ` and `Counter`

We can use Counter to count the number of "x" lists. Since we don’t want to count "x" more than once for each inner list, we convert each inner list to sets. Then combine these sets of elements into one sequence using ` chain.from_iterable () `.

 ` # Python3 Counting program ` ` # a list containing a specific element ` ` # in a list of lists ` ` from ` ` itertools ` ` import ` ` chain ` ` from ` ` collections ` ` import ` ` Counter `   ` def ` ` countList (lst, x): `   ` return ` ` Counter (chain.from_iterable (` ` set ` ` (i) ` ` for ` ` i ` ` in ` ` lst)) [x] `   ` Driver code ` ` lst ` ` = ` ` ([` ` ’a’ ` `], [` ` ’a’ ` `, ` `’ c’ ` `, ` ` ’b’ ` `], [` ` ’d’ ` `]) ` ` x ` ` = ` ` ’a’ ` ` print ` ` (countList (lst, x)) `

Exit :

` 2 `

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

NUMPYNUMPY

glob exclude pattern

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

NUMPYNUMPY

Python CSV error: line contains NULL byte

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries