# Python Program to Count Words that Appear Exactly Two Times in an Array/List of Words

In the previous article, we have discussed Python Program for Snake Case of a Given Sentence
The task is counting the words that appear exactly twice in an array/list of words in a given list in Python

Examples:

Example1:

Input:

Given List of words = ['hi', 'all', 'this', 'is', 'btechgeeks', 'hi', 'this', 'btechgeeks']

Output:

The Count of words in a given list ['hi', 'all', 'this', 'is', 'btechgeeks', 'hi', 'this', 'btechgeeks']  which are exactly repeated two times = 3

Example2:

Input:

Given List of words = ['good', 'morning', 'this', 'is', 'btechgeeks', 'good', 'is']

Output:

The Count of words in a given list ['good', 'morning', 'this', 'is', 'btechgeeks', 'good', 'is'] which are exactly repeated two times = 2

## Program to Count Words that Appear Exactly Two Times in an Array/List of Words

Below are the ways to count the words that appear exactly twice in an array/list of words in a given list.

### Method #1: Using For Loop (Static Input)

Approach:

• Import the Counter() function from collections using the import keyword.
• Give the list of words as static input and store it in a variable.
• Calculate the frequency of all the given list of words using the Counter() function which returns the element and its frequency as key-value pair and store this dictionary in a variable(say dictnry)
• Take a variable say countt and initialize its value with 0.
• Iterate in the above-obtained dictionary values using the for loop.
• Check if the iterator value is equal to 2 or not using the if conditional statement.
• If the statement is true, increase the value of countt by 1.
• Print the count of words that appear exactly twice in an array/list of words in a given list.
• The Exit of the Program.

Below is the implementation:

# Import the Counter() function from collections using the import keyword.
from collections import Counter
# Give the list of words as static input and store it in a variable.
gvn_lst = ["hi", "all", "this", "is", "btechgeeks", "hi", "this", "btechgeeks"]
# Calculate the frequency of all the given list of words using the Counter() function
# which returns the element and its frequency as key-value pair and store this
# dictionary in a variable(say dictnry)
dictnry = Counter(gvn_lst)
# Take a variable say countt and initialize its value with 0.
countt = 0
# Iterate in the above-obtained dictionary values using the for loop.
for itr in dictnry.values():
# Check if the iterator value is equal to 2 or not using the if conditional statement.
if itr == 2:
# If the statement is true, increase the value of "countt" by 1.
countt += 1
# Print the count of words that appear exactly twice in an array/list of words in a
# given list.
print("The Count of words in a given list", gvn_lst,
" which are exactly repeated two times =", countt)


Output:

The Count of words in a given list ['hi', 'all', 'this', 'is', 'btechgeeks', 'hi', 'this', 'btechgeeks']  which are exactly repeated two times = 3

### Method #2: Using For loop (User Input)

Approach:

• Import the Counter() function from collections using the import keyword.
• Give the list of words as user input using list(),map(),input(),and split() functions.
• Store it in a variable.
• Calculate the frequency of all the given list of words using the Counter() function which returns the element and its frequency as key-value pair and store this dictionary in a variable(say dictnry)
• Take a variable say countt and initialize its value with 0.
• Iterate in the above-obtained dictionary values using the for loop.
• Check if the iterator value is equal to 2 or not using the if conditional statement.
• If the statement is true, increase the value of countt by 1.
• Print the count of words that appear exactly twice in an array/list of words in a given list.
• The Exit of the Program.

Below is the implementation:

# Import the Counter() function from collections using the import keyword.
from collections import Counter
# Give the list of words as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(input(
'Enter some random List Elements separated by spaces = ').split())
# Calculate the frequency of all the given list of words using the Counter() function
# which returns the element and its frequency as key-value pair and store this
# dictionary in a variable(say dictnry)
dictnry = Counter(gvn_lst)
# Take a variable say countt and initialize its value with 0.
countt = 0
# Iterate in the above-obtained dictionary values using the for loop.
for itr in dictnry.values():
# Check if the iterator value is equal to 2 or not using the if conditional statement.
if itr == 2:
# If the statement is true, increase the value of "countt" by 1.
countt += 1
# Print the count of words that appear exactly twice in an array/list of words in a
# given list.
print("The Count of words in a given list", gvn_lst,
" which are exactly repeated two times =", countt)


Output:

Enter some random List Elements separated by spaces = good morning this is btechgeeks good is
The Count of words in a given list ['good', 'morning', 'this', 'is', 'btechgeeks', 'good', 'is'] which are exactly repeated two times = 2

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.