Wednesday, June 10, 2026
HomeLanguagesPython – Check if Splits are equal

Python – Check if Splits are equal

Given String separated by delim, check if all splits are similar.

Input : ’45#45#45′, delim = ‘#’ Output : True Explanation : All equal to 45. Input : ‘4@5@5’, delim = ‘@’ Output : False Explanation : 1st segment equal to 4, rest 5.

Method #1 : Using set() + len() + split()

In this, we perform split to get elements in list format, then convert to set, duplicates are removed, and check for len == 1, confirms all elements are same.

Python3




# Python3 code to demonstrate working of
# Check if Splits are equal
# Using set() + len() + split()
 
# initializing string
test_str = '45# 45# 45# 45# 45'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing splt_chr
splt_chr = "#"
 
# checking for length of set obtained, res stores boolean result
res = len(list(set(test_str.split(splt_chr)))) == 1
     
# printing result
print("Are all splits equal ? : " + str(res))


Output

The original string is : 45#45#45#45#45
Are all splits equal ? : True

Time complexity: O(n), where n is the length of the test_list. The set() + len() + split() takes O(n) time
Auxiliary Space: O(1), extra constant space is required

Method #2 : Using split() + all()

In this, we perform task of checking for all elements equal using all().

Python3




# Python3 code to demonstrate working of
# Check if Splits are equal
# Using split() + all()
 
# initializing string
test_str = '45# 45# 45# 45# 45'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing splt_chr
splt_chr = "#"
 
# splitting using split()
new_list = test_str.split(splt_chr)
 
# checking all equal to 1st element
res = all(ele == new_list[0] for ele in new_list)
     
# printing result
print("Are all splits equal ? : " + str(res))


Output

The original string is : 45#45#45#45#45
Are all splits equal ? : True
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7018 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS