Python: Check if first digit/character in each element in a given list is same or not
Python List: Exercise - 204 with Solution
Check Common First Digit/Character
Write a Python program to check if the first digit or character of each element in a list is the same.
Visual Presentation:
Sample Solution:
Python Code:
# Define a function 'test' that checks if the first character (or digit) in each element of the given list is the same.
def test(lst):
# Use a generator expression with 'all' to check if the first character (or digit) of each element matches the first element.
result = all(str(x)[0] == str(lst[0])[0] for x in lst)
return result
# Define a list 'nums' containing integers.
nums = [1234, 122, 1984, 19372, 100]
# Print a message indicating the original list.
print("\nOriginal list:")
# Print the original list 'nums'.
print(nums)
# Print a message indicating the purpose of the following lines of code.
print("Check if the first digit in each element of the said given list is the same or not!")
# Call the 'test' function to check if the first digit in each element is the same and print the result.
print(test(nums))
# Define a new list 'nums' with different integer elements.
nums = [1234, 922, 1984, 19372, 100]
# Print a message indicating the original list.
print("\nOriginal list:")
# Print the new list 'nums'.
print(nums)
# Print a message indicating the purpose of the following lines of code.
print("Check if the first digit in each element of the said given list is the same or not!")
# Call the 'test' function again with the new list 'nums' and print the result.
print(test(nums))
# Define a list 'nums' containing string elements.
nums = ['aabc', 'abc', 'ab', 'a']
# Print a message indicating the original list.
print("\nOriginal list:")
# Print the original list 'nums'.
print(nums)
# Print a message indicating the purpose of the following lines of code.
print("Check if the first character in each element of the said given list is the same or not!")
# Call the 'test' function to check if the first character in each element is the same and print the result.
print(test(nums))
# Define a new list 'nums' with different string elements.
nums = ['aabc', 'abc', 'ab', 'ha']
# Print a message indicating the original list.
print("\nOriginal list:")
# Print the new list 'nums'.
print(nums)
# Print a message indicating the purpose of the following lines of code.
print("Check if the first character in each element of the said given list is the same or not!")
# Call the 'test' function again with the new list 'nums' and print the result.
print(test(nums))
Sample Output:
Original list: [1234, 122, 1984, 19372, 100] Check if first digit in each element of the said given list is same or not! True Original list: [1234, 922, 1984, 19372, 100] Check if first digit in each element of the said given list is same or not! False Original list: ['aabc', 'abc', 'ab', 'a'] Check if first character in each element of the said given list is same or not! True Original list: ['aabc', 'abc', 'ab', 'ha'] Check if first character in each element of the said given list is same or not! False
Flowchart:
Python Code Editor:
Previous: Write a Python program to join adjacent members of a given list.
Next: Write a Python program to find the indices of elements of a given list, greater than a specified value.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/list/python-data-type-list-exercise-204.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics