2
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

James Z
  • 12,209
  • 10
  • 24
  • 44

3 Answers3

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))
Joe john
  • 9
  • 9
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
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
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.

user2390182
  • 72,016
  • 6
  • 67
  • 89