 Open in App
Not now

# Python – Similar index elements frequency

• Last Updated : 20 Mar, 2023

Sometimes, while working with Python list, we can have a problem in which we need to check if one element has similar index occurrence in other list. This can have possible application in many domains. Lets discuss certain ways in which this task can be performed.

Method #1 : Using sum() + zip() The combination of above functions can be used to perform this task. In this, we perform summation of elements that after zipping cross lists together match.

## Python3

 `# Python3 code to demonstrate``# Similar index elements frequency``# using sum() + zip()` `# Initializing lists``test_list1 ``=` `[``1``, ``3``, ``5``, ``6``, ``8``]``test_list2 ``=` `[``4``, ``3``, ``6``, ``6``, ``10``]` `# printing original lists``print``("The original ``list` `1` `is` `: " ``+` `str``(test_list1))``print``("The original ``list` `2` `is` `: " ``+` `str``(test_list2))` `# Similar index elements frequency``# using sum() + zip()``res ``=` `sum``(x ``=``=` `y ``for` `x, y ``in` `zip``(test_list1, test_list2))` `# printing result``print` `("Number of elements having similar index : " ``+` `str``(res))`

Output :

```The original list 1 is : [1, 3, 5, 6, 8]
The original list 2 is : [4, 3, 6, 6, 10]
Number of elements having similar index : 2```

Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the new res list

Method #2 : Using list comprehension + enumerate() The combination of above functionalities can be used to perform this task. In this, we iterate through each element in lists and increase the sum accordingly.

## Python3

 `# Python3 code to demonstrate``# Similar index elements frequency``# using list comprehension + enumerate()` `# Initializing lists``test_list1 ``=` `[``1``, ``3``, ``5``, ``6``, ``8``]``test_list2 ``=` `[``4``, ``3``, ``6``, ``6``, ``10``]` `# printing original lists``print``("The original ``list` `1` `is` `: " ``+` `str``(test_list1))``print``("The original ``list` `2` `is` `: " ``+` `str``(test_list2))` `# Similar index elements frequency``# using list comprehension + enumerate()``res ``=` `len``([key ``for` `key, val ``in` `enumerate``(``zip``(test_list1, test_list2)) ``if` `val[``0``] ``=``=` `val[``1``]])` `# printing result``print` `("Number of elements having similar index : " ``+` `str``(res))`

Output :

```The original list 1 is : [1, 3, 5, 6, 8]
The original list 2 is : [4, 3, 6, 6, 10]
Number of elements having similar index : 2```

Method #3 : Using map() + itertools zip_longest()

## Python3

 `#Python3 code to demonstrate``#Similar index elements frequency``#using map() + zip_longest()``#importing itertools module``from` `itertools ``import` `zip_longest` `#Initializing lists``test_list1 ``=` `[``1``, ``3``, ``5``, ``6``, ``8``]``test_list2 ``=` `[``4``, ``3``, ``6``, ``6``, ``10``]` `#printing original lists``print``(``"The original list 1 is : "` `+` `str``(test_list1))``print``(``"The original list 2 is : "` `+` `str``(test_list2))` `#Similar index elements frequency``#using map() + zip_longest()``res ``=` `sum``(``map``(``lambda` `x: x[``0``] ``=``=` `x[``1``], zip_longest(test_list1, test_list2, fillvalue``=``-``1``)))` `#printing result``print` `(``"Number of elements having similar index : "` `+` `str``(res))`

Output

```The original list 1 is : [1, 3, 5, 6, 8]
The original list 2 is : [4, 3, 6, 6, 10]
Number of elements having similar index : 2```

Time Complexity: O(n) where n is the length of the longest list.
Auxiliary Space: O(1) as we are only using a single integer to store the result.
In this approach, we first import the zip_longest() method from itertools module and then use the map() function to apply a lambda function that compares elements at each index of both the lists and then we use the sum() method to find the sum of all comparisons (True or False). The fillvalue argument in zip_longest is used to fill values in the shorter list if they are less elements than the longer list, so that they can be compared.

This approach gives us a concise way to find the number of elements with the same index in both the lists and its time complexity is O(n) where n is the length of the longest list and its space complexity is O(1) as we are only using a single integer to store the result.

Method #4:Using for loop

## Python3

 `# Initializing lists``test_list1 ``=` `[``1``, ``3``, ``5``, ``6``, ``8``]``test_list2 ``=` `[``4``, ``3``, ``6``, ``6``, ``10``]` `# printing original lists``print``(``"The original list 1 is : "` `+` `str``(test_list1))``print``(``"The original list 2 is : "` `+` `str``(test_list2))` `# Using for loop``count ``=` `0``for` `i ``in` `range``(``len``(test_list1)):``    ``if` `test_list1[i] ``=``=` `test_list2[i]:``        ``count ``+``=` `1` `# printing result``print``(``"Number of elements having similar index : "` `+` `str``(count))``#This code is contributed by Vinay Pinjala.`

Output

```The original list 1 is : [1, 3, 5, 6, 8]
The original list 2 is : [4, 3, 6, 6, 10]
Number of elements having similar index : 2```

Time Complexity: O(n)

Auxiliary Space: O(1)

Method#5: Using Recursive method.

Algorithm:

1. Check if the index is equal to the length of the first list. If it is, return 0.
2. Check if the current element in both lists is equal. If it is, recursively call the count_matching_elements() function with the next index and add 1 to the result.
3. If the current element in both lists is not equal, recursively call the count_matching_elements() function with the next index.
4. Continue this process until the function has compared all the elements in the two lists.
5. Return the number of elements that have the same index and are equal in both lists.

## Python3

 `def` `count_matching_elements(list1, list2, index``=``0``):``    ``if` `index ``=``=` `len``(list1):``        ``return` `0``    ``elif` `list1[index] ``=``=` `list2[index]:``        ``return` `1` `+` `count_matching_elements(list1, list2, index ``+` `1``)``    ``else``:``        ``return` `count_matching_elements(list1, list2, index ``+` `1``)` `# Initializing lists``test_list1 ``=` `[``1``, ``3``, ``5``, ``6``, ``8``]``test_list2 ``=` `[``4``, ``3``, ``6``, ``6``, ``10``]` `# printing original lists``print``(``"The original list 1 is : "` `+` `str``(test_list1))``print``(``"The original list 2 is : "` `+` `str``(test_list2))` `# Using recursive method``count ``=` `count_matching_elements(test_list1,test_list2)` `# printing result``print``(``"Number of elements having similar index : "` `+` `str``(count))``#This code is contributed by tvsk.`

Output

```The original list 1 is : [1, 3, 5, 6, 8]
The original list 2 is : [4, 3, 6, 6, 10]
Number of elements having similar index : 2```

Time complexity:
The time complexity of the function is O(n), where n is the length of the input lists. This is because the function needs to compare each element in the two lists once. Since the function visits each element once, the time complexity is linear in the length of the input.

Auxiliary space:
The auxiliary space complexity of the function is O(n), where n is the maximum depth of the recursion. This is because the function creates a new stack frame for each recursive call. The maximum depth of the recursion is equal to the length of the input lists. Therefore, the space complexity of the function is also linear in the length of the input.

My Personal Notes arrow_drop_up