# Python | Finding frequency in list of tuples

• Last Updated : 16 Jan, 2023

In python we need to handle various forms of data and one among them is list of tuples in which we may have to perform any kind of operation. This particular article discusses the ways of finding the frequency of the 1st element in list of tuple which can be extended to any index. Let’s discuss certain ways in which this can be performed.

Method #1 : Using map() + count() The map function can be used to accumulate the indices of all the tuples in a list and the task of counting the frequency can be done using the generic count function of python library.

## Python3

 `# Python3 code to demonstrate``# finding frequency in list of tuples``# using map() + count()` `# initializing list of tuples``test_list ``=` `[(``'Geeks'``, ``1``), (``'for'``, ``2``), (``'Geeks'``, ``3``)]` `# printing the original list``print` `("The original ``list` `is` `: " ``+` `str``(test_list))` `# using map() + count()``# finding frequency in list of tuples``res ``=` `list``(``map``(``lambda` `i : i[``0``], test_list)).count(``'Geeks'``)` `# printing result``print` `("The frequency of element ``is` `: " ``+` `str``(res))`

Method #2 : Using Counter() + list comprehension List comprehension performs the task of getting the first element of the tuples and the counting part is handled by Counter function of collection library.

## Python3

 `# Python3 code to demonstrate``# finding frequency in list of tuples``# using Counter() + list comprehension``from` `collections ``import` `Counter` `# initializing list of tuples``test_list ``=` `[(``'Geeks'``, ``1``), (``'for'``, ``2``), (``'Geeks'``, ``3``)]` `# printing the original list``print` `("The original ``list` `is` `: " ``+` `str``(test_list))` `# using Counter() + list comprehension``# finding frequency in list of tuples``res ``=` `Counter(i[``0``] ``for` `i ``in` `test_list)` `# printing result``print` `("The frequency of element ``is` `: " ``+` `str``(res[``'Geeks'``]))`

Method #3 : Dictionary comprehension and the get() method

To use dictionary comprehension and the get() method to find the frequency of the element ‘Geeks’ in the list of tuples test_list, you can do the following:

## Python3

 `# initializing list of tuples``test_list ``=` `[(``'Geeks'``, ``1``), (``'for'``, ``2``), (``'Geeks'``, ``3``)]``# create a dictionary to store the frequency of each element looping and the get() method``frequency ``=` `{}``for` `element ``in` `test_list:``    ``frequency[element[``0``]] ``=` `frequency.get(element[``0``], ``0``) ``+` `1``# print the frequency of the element 'Geeks'``print``(frequency[``'Geeks'``])``#This code is contributed by Edula Vinay Kumar Reddy`

Output

`2`

If the element is already in the dictionary, the get() method returns its current value, and this value is incremented by 1. This allows us to count the frequency of each element in the list of tuples.

Time complexity: O(n)

Auxiliary Space: O(n)

Method #4 : Using map() + operator.countOf() method

The map function can be used to accumulate the indices of all the tuples in a list and the task of counting the frequency can be done using the operator.countOf function of python library.

## Python3

 `# Python3 code to demonstrate``# finding frequency in list of tuples``# using map() + operator.countOf()``import` `operator as op``# initializing list of tuples``test_list ``=` `[(``'Geeks'``, ``1``), (``'for'``, ``2``), (``'Geeks'``, ``3``)]` `# printing the original list``print` `(``"The original list is : "` `+` `str``(test_list))` `# using map() + operator.countOf()``# finding frequency in list of tuples``res ``=` `op.countOf(``list``(``map``(``lambda` `i : i[``0``], test_list)),``'Geeks'``)` `# printing result``print` `(``"The frequency of element is : "` `+` `str``(res))`

Output

```The original list is : [('Geeks', 1), ('for', 2), ('Geeks', 3)]
The frequency of element is : 2```

Time complexity: O(n*n)

Auxiliary Space: O(1)

My Personal Notes arrow_drop_up