Get the best out of our app
GeeksforGeeks App
Open App
Browser
Continue

# Python | Checking if starting digits are similar in list

Sometimes we may face a problem in which we need to find a list if it contains numbers 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 starting 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 first element of string.

## Python3

 `# Python3 code to demonstrate``# checking for first digit in elements``# using list comprehension + map()` `# initializing list``test_list ``=` `[``45``, ``4``, ``428829``, ``432``]` `# printing original list``print``(``"The original list : "` `+` `str``(test_list))` `# using list comprehension + map()``# checking for first digit in elements``res ``=` `len``(``set``(sub[``0``] ``for` `sub ``in` `map``(``str``, test_list))) ``=``=` `1` `# print result``print``(``"Does each element start with same digit ? "` `+` `str``(res))`

Output

```The original list : [45, 4, 428829, 432]
Does each element start with same digit ? True```

Time Complexity: O(n) where n is the number of elements in the list.
Auxiliary Space: O(n) as we are storing the first digit of each element in a set.

Method #2: Using all() + list comprehension

This is yet another approach in which this problem can be solved. In this, we use all function 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 first digit of first element.

## Python3

 `# Python3 code to demonstrate``# checking for first digit in elements``# using all() + list comprehension` `# initializing list``test_list ``=` `[``45``, ``4``, ``428829``, ``432``]` `# printing original list``print``(``"The original list : "` `+` `str``(test_list))` `# using all() + list comprehension``# checking for first digit in elements``res ``=` `all``(``str``(i)[``0``] ``=``=` `str``(test_list[``0``])[``0``] ``for` `i ``in` `test_list)` `# print result``print``(``"Does each element start with same digit ? "` `+` `str``(res))`

Output

```The original list : [45, 4, 428829, 432]
Does each element start with same digit ? True```

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(1), as the space used is constant regardless of the input size.

Method #3: Using startswith() method

## Python3

 `# Python3 code to demonstrate``# checking for first digit in elements` `# initializing list``test_list ``=` `[``45``, ``4``, ``428829``, ``432``]``p``=``list``(``map``(``str``,test_list))``# printing original list``print``(``"The original list : "` `+` `str``(test_list))``c``=``0``x``=``p[``0``][``0``]``for` `i ``in` `p:``    ``if``(i.startswith(x)):``        ``c``+``=``1``res``=``False``if``(c``=``=``len``(p)):``    ``res``=``True``    ` `# print result``print``(``"Does each element start with same digit ? "` `+` `str``(res))`

Output

```The original list : [45, 4, 428829, 432]
Does each element start with same digit ? True```

Time complexity: O(n) – The code iterates through each element of the list once.
Auxiliary space: O(n) – The code creates a new list ‘p’ with the same number of elements as the original list.

Method #4 : Using for loop,len() method

## Python3

 `# Python3 code to demonstrate``# checking for first digit in elements` `# initializing list``test_list ``=` `[``45``, ``4``, ``428829``, ``432``]` `p ``=` `list``(``map``(``str``, test_list))` `# printing original list``print``(``"The original list : "` `+` `str``(test_list))` `c ``=` `0``x ``=` `[p[``0``][``0``]]``*``len``(test_list)``y ``=` `[]``for` `i ``in` `p:``    ``y.append(i[``0``])` `res ``=` `False``if``(x ``=``=` `y):``    ``res ``=` `True` `# print result``print``(``"Does each element start with same digit ? "` `+` `str``(res))`

Output

```The original list : [45, 4, 428829, 432]
Does each element start with same digit ? True```

Method #5 : Using re

The code initializes a list test_list with four elements. Then it prints the original list to the console.

Next, it uses a list comprehension to extract the first digit from each element in the test_list. It does this by using the findall() function from the re module to match a single digit in the string representation of each element, and then it gets the first element from the resulting list. The resulting list is stored in the first_digits variable.

Finally, it uses the len() and set() functions to check if all the elements in the first_digits list are the same. It does this by converting the first_digits list to a set, which removes any duplicates, and then checking if the length of the set is equal to 1. The result is stored in the res variable, which is a Boolean value indicating whether all the elements in the first_digits list are the same. The result is printed to the console.

Here is an example of how this can be implemented:

## Python3

 `import` `re` `# initializing list``test_list ``=` `[``45``, ``4``, ``428829``, ``432``]` `# printing original list``print``(``"The original list:"``, test_list)` `# extracting digits from elements``first_digits ``=` `[re.findall(r``'\d'``, ``str``(i))[``0``] ``for` `i ``in` `test_list]` `# checking if all digits are the same``res ``=` `len``(``set``(first_digits)) ``=``=` `1` `# print result``print``(``"Does each element contain the same digits?"``, res)` `#This code is contributed by Edula Vinay Kumar Reddy`

Output

```The original list: [45, 4, 428829, 432]
Does each element contain the same digits? True```

Time complexity: O(n), where n is the length of the test_list, because the list comprehension iterates over the elements of the test_list once, and the len() and set() functions each iterate over the elements of the first_digits list once.
Auxiliary space: O(n), because the first_digits list grows by one element for each iteration of the list comprehension, and the set() function creates a new set object with a capacity equal to the number of elements in the first_digits list.

Method #6 : Using str() and count() methods

1. Initiated a for loop to traverse the list
2. Converted element to string and appended the first character to a list
3. Checked if the count of first element of list to length of list(using count())
4. If True assign True to res variable
5. Display res variable

## Python3

 `# Python3 code to demonstrate``# checking for first digit in elements` `# initializing list``test_list ``=` `[``45``, ``4``, ``428829``, ``432``]` `# printing original list``print``(``"The original list : "` `+` `str``(test_list))``# checking for first digit in elements``res``=``False``y``=``[]``for` `i ``in` `test_list:``    ``x``=``str``(i)``    ``y.append(x[``0``])``if``(y.count(y[``0``])``=``=``len``(y)):``    ``res``=``True` `# print result``print``(``"Does each element start with same digit ? "` `+` `str``(res))`

Output

```The original list : [45, 4, 428829, 432]
Does each element start with same digit ? True```

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

Method #7: Using set comprehension and string slicing

This method uses a set comprehension to extract the first digit of each element in the list and stores them in a set. Then it checks whether the length of the set is equal to 1, which means all elements have the same first digit.

## Python3

 `# initializing list``test_list ``=` `[``45``, ``4``, ``428829``, ``432``]` `# printing original list``print``(``"The original list : "` `+` `str``(test_list))` `# checking for first digit in elements``first_digits ``=` `{``str``(i)[``0``] ``for` `i ``in` `test_list}``res ``=` `len``(first_digits) ``=``=` `1` `# print result``print``(``"Does each element start with same digit? "` `+` `str``(res))`

Output

```The original list : [45, 4, 428829, 432]
Does each element start with same digit? True```

Time complexity: O(n), where n is the number of elements in the list.
Auxiliary space: O(n), since we need to store the first digit of each element in a set.

Method #8: Using a while loop and integer division

Algorithm:

• Initialize a variable ‘first_digit’ with the first digit of the first element in the list.
• Use a while loop to iterate through the remaining elements of the list and for each element, get its first digit by dividing it by 10 until the quotient is less than 10.
• Check if the first digit is equal to ‘first_digit‘. If not, return False.
• If the loop completes without returning False, return True.

Implementation:

## Python3

 `# Initialize the list``test_list ``=` `[``45``, ``4``, ``428829``, ``432``]` `# Print original list``print``(``"The original list : "` `+` `str``(test_list))` `# Check for first digit in elements``first_digit ``=` `test_list[``0``] ``/``/` `10``*``*``(``len``(``str``(test_list[``0``]))``-``1``)` `for` `i ``in` `range``(``1``, ``len``(test_list)):``    ``num ``=` `test_list[i]``    ``while` `num >``=` `10``:``        ``num ``/``/``=` `10``    ``if` `num !``=` `first_digit:``        ``res ``=` `False``        ``break``else``:``    ``res ``=` `True` `# Print result``print``(``"Does each element start with same digit? "` `+` `str``(res))`

Output

```The original list : [45, 4, 428829, 432]
Does each element start with same digit? True```

Time Complexity: O(n*m), where n is the length of the list and m is the number of digits in the largest element.
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up