1
String1 = “Python is an interpreted high level general purpose programming language”
List1 = String1.split()

for i in List1:
    for j in i:
        if i.count(j)>1:
            List1.remove(i)
            break
    
print(“ “.join(List1))

output:

Python is an high general programming

Expected output:

Python is an

Let me know the mistake I am making

3 Answers 3

0

try this code

String1 = "Python is an interpreted high level general purpose programming language"
List1 = String1.split()
ResultList = []

for i in List1:
    has_repeated_letter = False
    for j in i:
        if i.count(j) > 1:
            has_repeated_letter = True
            break
    if not has_repeated_letter:
        ResultList.append(i)

print(" ".join(ResultList))
Sign up to request clarification or add additional context in comments.

Comments

0

Using a regex approach:

inp = "Python is an interpreted high level general purpose programming language"
words = inp.split()
output = [x for x in words if not re.search(r'(\w)(?=.*\1)', x)]
print(output)  # ['Python', 'is', 'an']

The regex pattern used here will match any word for which any character occurs two or more times:

  • (\w) match any single letter and capture in \1
  • (?=.*\1) assert that this same character occurs later in the word

Comments

0

One problem is removing from a list while iterating it. It is almost always more useful to rebuild the list with the elements you do want:

List1 = [i for i in List1 if len(i) == len(set(i))]
# ['Python', 'is', 'an']

Turning the strings into sets removes duplicate letters. Hence the length comparison with the original string. You retain only those with only unique letters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.