Sometimes, while working with Python tuples, we can have a problem in which we need to extract the frequency of each value in tuple. This has been solved earlier. We can have a modification in which we need to create list in which index represents the key and value of it represents the frequency of that index number. This kind of problem can have applications in competitive programming domain. Let’s discuss certain ways in which we need to solve this problem.

Input: test_list = [(‘Gfg’, 1), (‘is’, 1), (‘best’, 1), (‘for’, 1), (‘geeks’, 1)]

Output: [0, 5, 0, 0, 0, 0]

Input: test_list = [(‘Gfg’, 5), (‘is’, 5)]

Output: [0, 0, 0, 0, 0, 2]

**Method #1 : Using loop**

This is brute force approach by which this task can be performed. In this, we perform the task of assigning frequency by iterating and assigning pre-initialized list.

`# Python3 code to demonstrate working of ` `# Values Frequency Index List ` `# Using loop ` ` ` `# initializing list ` `test_list ` `=` `[(` `'Gfg'` `, ` `3` `), (` `'is'` `, ` `3` `), (` `'best'` `, ` `1` `), (` `'for'` `, ` `5` `), (` `'geeks'` `, ` `1` `)] ` ` ` `# printing original list ` `print` `(` `"The original list is : "` `+` `str` `(test_list)) ` ` ` `# Values Frequency Index List ` `# Using loop ` `res ` `=` `[` `0` `for` `_ ` `in` `range` `(` `6` `)] ` `for` `ele ` `in` `test_list: ` ` ` `res[ele[` `1` `]] ` `=` `res[ele[` `1` `]] ` `+` `1` ` ` `# printing result ` `print` `(` `"The Frequency list : "` `+` `str` `(res)) ` |

*chevron_right*

*filter_none*

**Output :**

The original list is : [('Gfg', 3), ('is', 3), ('best', 1), ('for', 5), ('geeks', 1)] The Frequency list : [0, 2, 0, 2, 0, 1]

**Method #2 : Using Counter() + list comprehension**

The combination of above functionalities is used to solve this problem. In this, we perform the task of computing frequencies using Counter() and rendering in list is done by list comprehension.

`# Python3 code to demonstrate working of ` `# Values Frequency Index List ` `# Using Counter() + list comprehension ` `from` `collections ` `import` `Counter ` ` ` `# initializing list ` `test_list ` `=` `[(` `'Gfg'` `, ` `3` `), (` `'is'` `, ` `3` `), (` `'best'` `, ` `1` `), (` `'for'` `, ` `5` `), (` `'geeks'` `, ` `1` `)] ` ` ` `# printing original list ` `print` `(` `"The original list is : "` `+` `str` `(test_list)) ` ` ` `# Values Frequency Index List ` `# Using Counter() + list comprehension ` `cntr ` `=` `Counter(ele[` `1` `] ` `for` `ele ` `in` `test_list) ` `res ` `=` `[cntr[idx] ` `for` `idx ` `in` `range` `(` `6` `)] ` ` ` `# printing result ` `print` `(` `"The Frequency list : "` `+` `str` `(res)) ` |

*chevron_right*

*filter_none*

**Output :**

The original list is : [('Gfg', 3), ('is', 3), ('best', 1), ('for', 5), ('geeks', 1)] The Frequency list : [0, 2, 0, 2, 0, 1]

Attention geek! Strengthen your foundations with the **Python Programming Foundation** Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the **Python DS** Course.

## Recommended Posts:

- Python - Index Frequency Alphabet List
- Python - Dictionary List Values Frequency
- Python | Initializing dictionary with list index values
- Python | Initializing dictionary with list index-values
- Python - Retain K match index values from other list
- Python - Assign values to Values List
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Python - Similar index elements frequency
- Python - False values Frequency
- Python | Values Frequency till Maximum K
- Python - Values frequency across Dictionaries lists
- Python - Keys Values equal frequency
- Generate a list using given frequency list
- How to count the frequency of unique values in NumPy array?
- Python | Sort list of list by specified index
- Python | Replace elements in second list with index of same element in first list
- Python | Add list elements with a multi-list based on index
- Python - Sort dictionaries list by Key's Value list index
- Python - Filter the List of String whose index in second List contaons the given Substring
- Sort the values of first list using second list in Python

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.