# Python – Test rear digit match in all list elements

Sometimes we may face a problem in which we need to find a list if it contains numbers ending with the same digits. This particular utility has an application in day-day programming. Letâ€™s discuss certain ways in which this task can be achieved.

Method #1: Using list comprehension + map()

We can approach this problem by converting the elements to the strings and then testing the ending element of string and if they are equal we can return true and then convert to set and test for size of result to be one. The conversion is done by map, set function converts to set and list comprehension checks for last element of the string.

## Python3

 `# Python3 code to demonstrate``# Test rear digit match``# using list comprehension + map()` `# initializing list``test_list ``=` `[``45``, ``545``, ``2345``, ``8765``]` `# printing original list``print``(``"The original list : "` `+` `str``(test_list))` `# using list comprehension + map()``# Test rear digit match``res ``=` `len``(``set``(sub[``-``1``] ``for` `sub ``in` `map``(``str``, test_list))) ``=``=` `1` `# print result``print``(``"Does each element end with same digit ? "` `+` `str``(res))`

Output
```The original list : [45, 545, 2345, 8765]
Does each element end with same digit ? True```

Time Complexity: O(n)

Auxiliary Space: O(n)

Method #2: Using all() + list comprehension This is yet another approach in which this problem can be solved. In this, we use all functions to check for all elements and return a Boolean result and list comprehension does the part of the conversion of string by str function and checking for all elements with the last digit of the last element.

## Python3

 `# Python3 code to demonstrate``# Test rear digit match``# using all() + list comprehension` `# initializing list``test_list ``=` `[``45``, ``545``, ``2345``, ``8765``]` `# printing original list``print``(``"The original list : "` `+` `str``(test_list))` `# using all() + list comprehension``# Test rear digit match``res ``=` `all``(``str``(i)[``-``1``] ``=``=` `str``(test_list[``-``1``])[``-``1``] ``for` `i ``in` `test_list)` `# print result``print``(``"Does each element end with same digit ? "` `+` `str``(res))`

Output
```The original list : [45, 545, 2345, 8765]
Does each element end with same digit ? True```

Time Complexity: O(n)

Auxiliary Space: O(1)

Method #3 : Using len() method and * operator

## Python3

 `# Python3 code to demonstrate``# Test rear digit match` `# initializing list ``test_list ``=` `[``45``, ``545``, ``2345``, ``8765``]` `# printing original list``print``(``"The original list : "` `+` `str``(test_list))``res``=``False``x``=``[]``for` `i ``in` `test_list:``    ``a``=``str``(i)[``-``1``]``    ``x.append(``int``(a))``y``=``[x[``0``]]``*``len``(x)``if` `y``=``=``x:``    ``res``=``True``# print result``print``(``"Does each element end with same digit ? "` `+` `str``(res))`

Output
```The original list : [45, 545, 2345, 8765]
Does each element end with same digit ? True```

Time Complexity: O(n)

Auxiliary Space: O(n)

Method #4: Using filter(),lambda function

## Python3

 `# Python3 code to demonstrate``# Test rear digit match` `# initializing list``test_list ``=` `[``45``, ``545``, ``2345``, ``8765``]` `# printing original list``print``(``"The original list : "` `+` `str``(test_list))``res ``=` `False``last_digit ``=` `str``(test_list[``0``])[``-``1``]``lis ``=` `list``(``filter``(``lambda` `x: ``str``(x)[``-``1``] ``=``=` `last_digit, test_list))``if``(``len``(lis) ``=``=` `len``(test_list)):``    ``res ``=` `True``# print result``print``(``"Does each element end with same digit ? "` `+` `str``(res))`

Output
```The original list : [45, 545, 2345, 8765]
Does each element end with same digit ? True```

Time Complexity: O(n)

Auxiliary Space: O(n)

Method 5:  Using Counter() method

## Python3

 `# Python3 code to demonstrate``# Test rear digit match``from` `collections ``import` `Counter``# initializing list``test_list ``=` `[``45``, ``545``, ``2345``, ``8765``]` `# printing original list``print``(``"The original list : "` `+` `str``(test_list))``res ``=` `False``x ``=` `[]``for` `i ``in` `test_list:``    ``a ``=` `str``(i)[``-``1``]``    ``x.append(``int``(a))` `freq ``=` `Counter(x)` `if``(``len``(freq) ``=``=` `1``):``    ``res ``=` `True``# print result``print``(``"Does each element end with same digit ? "` `+` `str``(res))`

Output
```The original list : [45, 545, 2345, 8765]
Does each element end with same digit ? True```

Time Complexity: O(N)
Auxiliary Space : O(N)

Method 6:  Using itertools.groupby() function:

## Python3

 `import` `itertools``test_list ``=` `[``45``, ``545``, ``2345``, ``8765``]``# printing original list``print``(``"The original list : "` `+` `str``(test_list))``res ``=` `len``(``list``(itertools.groupby(test_list, key``=``lambda` `x: x``%``10``))) ``=``=` `1``print``(``"Does each element end with same digit ? "` `+` `str``(res))``#This code is contributed by pinjala Jyothi.`

Output
```The original list : [45, 545, 2345, 8765]
Does each element end with same digit ? True```

Time Complexity: O(N)
Auxiliary Space : O(N)

Method #7: Using Recursive method.

STEPS:

1. Define a function called check_last_digit_match that takes two arguments: lst and last_digit.
2. Inside the function, check if the list is empty. If it is, return True since there are no elements to compare.
3. Check if last_digit is None. If it is, assign the last digit of the first element of lst to last_digit.
4. Check if the last digit of the first element of lst is not equal to last_digit. If it isn’t, return False.
5. If the first element of lst passes the last digit check, recursively call the check_last_digit_match function with the remaining elements of lst and last_digit.
6. When the recursive call completes, return the result.
7. Create a list called test_list and initialize it with four integers.
8. Print the original list.
9. Call the check_last_digit_match function with the test_list list as the argument and assign the result to a variable called result.
10. Print whether each element in the list ends with the same digit by printing Does each element end with same digit? followed by the result variable.

## Python3

 `# Python3 code to demonstrate``# Test rear digit match``def` `check_last_digit_match(lst, last_digit``=``None``):``    ``if` `not` `lst:``        ``return` `True``    ``if` `last_digit ``is` `None``:``        ``last_digit ``=` `str``(lst[``0``])[``-``1``]``    ``if` `str``(lst[``0``])[``-``1``] !``=` `last_digit:``        ``return` `False``    ``return` `check_last_digit_match(lst[``1``:], last_digit)` `# initializing list``test_list ``=` `[``45``, ``545``, ``2345``, ``8765``]``# printing original list``print``(``"The original list : "` `+` `str``(test_list))``result ``=` `check_last_digit_match(test_list)``# print result``print``(``"Does each element end with same digit? "``, result)``#this code contributed by tvsk`

Output
```The original list : [45, 545, 2345, 8765]
Does each element end with same digit?  True```

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #8 : Using count() and len() methods

Approach

1. Initiate a for loop to traverse test_list
2. Extract the last digit of each number using str(),int() and append to a new empty list x
3. Check whether the count of first element in list x is equal to length of x
4. If yes set res to True or else False

## Python3

 `# Python3 code to demonstrate``# Test rear digit match` `# initializing list``test_list ``=` `[``45``, ``545``, ``2345``, ``8765``]` `# printing original list``print``(``"The original list : "` `+` `str``(test_list))``res``=``False``x``=``[]``for` `i ``in` `test_list:``    ``a``=``str``(i)[``-``1``]``    ``x.append(``int``(a))` `res``=``x.count(x[``0``])``=``=``len``(x)``# print result``print``(``"Does each element end with same digit ? "` `+` `str``(res))`

Output
```The original list : [45, 545, 2345, 8765]
Does each element end with same digit ? True```

Time Complexity: O(N)

Auxiliary Space : O(1)

Previous
Next