 Open in App
Not now

# Python | Tuple Column element frequency

• Last Updated : 09 Mar, 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 Kth element in list of tuple. 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``# Tuple Column element frequency``# using map() + count()` `# initializing list of tuples``test_list ``=` `[(``1``, ``'Geeks'``), (``2``, ``'for'``), (``3``, ``'Geeks'``)]` `# printing the original list``print` `("The original ``list` `is` `: " ``+` `str``(test_list))` `# initializing K``K ``=` `1` `# using map() + count()``# Tuple Column element frequency``res ``=` `list``(``map``(``lambda` `i : i[K], test_list)).count(``'Geeks'``)` `# printing result``print` `("The frequency of element at Kth index ``is` `: " ``+` `str``(res))`

Output :

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

Time complexity: O(n),
Auxiliary space: O(1).

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

## Python3

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

Output :

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

Time complexity: O(n), where n is the length of the input list.

Space complexity: O(k), where k is the number of unique elements at the Kth index of the input list.

Method 3:using a simple for loop

using a simple for loop to iterate through the list of tuples and maintain a dictionary to keep track of the frequency of elements at Kth index.

## Python3

 `# initializing list of tuples``test_list ``=` `[(``1``, ``'Geeks'``), (``2``, ``'for'``), (``3``, ``'Geeks'``)]` `# initializing K``K ``=` `1` `# using for loop to count frequency``freq_dict ``=` `{}``for` `tup ``in` `test_list:``    ``if` `tup[K] ``in` `freq_dict:``        ``freq_dict[tup[K]] ``+``=` `1``    ``else``:``        ``freq_dict[tup[K]] ``=` `1` `# printing result``print``(``"The frequency of element at Kth index is :"``, freq_dict[``'Geeks'``])`

Output

```The frequency of element at Kth index is : 2
```

The time complexity of this approach is O(n), where n is the length of the list of tuples.

The space complexity is O(k), where k is the number of distinct elements at Kth index.

My Personal Notes arrow_drop_up