-1

if input is 'ameer bau' then output should be 'mr b' but i am getting error 'index out of range' my code is below

str=input()
new=list(str)

for i in range(len(new)):
    if new[i]=='a' or new[i]=='e' or new[i]=='i' or new[i]=='o' or new[i]=='u':
        new.pop(i)

str1="".join(new)
print(str1)
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
FLASH
  • 1
  • 2
  • Possible duplicate of [How to remove items from a list while iterating?](https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating) – Tobias Wilfert Dec 17 '18 at 15:40
  • The three rules of writing small programs: 1. Debug 2. Debug 3. Debug https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ –  Dec 17 '18 at 15:50

5 Answers5

3

In your for loop you iterate over the indices of new, however in the body of the loop new.pop(i) is called. That changes the size of the list eventually causing the IndexError.

Instead, use a list comprehension (if you need the list) or a generator expression:

string = input()
vowels = {'a', 'e', 'i', 'o', 'u'}
new_string = ''.join(x for x in string if x not in vowels)
print(new_string)
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
1
string = 'ameer bau'
new_string = ''

for letter in string:
    if letter not in 'aeiou':
        new_string += letter

print (new_string) 
# mr b

To change it to exactly fit your question:

string = input('Enter your string here: ')
new_string = ''

for letter in string:
    if letter not in 'aeiou':
        new_string += letter

print (new_string) 
# mr b

Also, str is a reserved keyword, you should not use it as a variable name

ycx
  • 3,155
  • 3
  • 14
  • 26
  • 2
    Why are you using `_` as a loop variable if you're referencing it elsewhere? – Patrick Haugh Dec 17 '18 at 15:34
  • @PatrickHaugh I do not understand your question. Please clarify – ycx Dec 17 '18 at 15:36
  • 1
    `_` is usually used to indicate that you don't care about the value of an expression, but Python requires you to assign it. For readability, you should give the loop variable a meaningful name. – Patrick Haugh Dec 17 '18 at 15:39
  • @PatrickHaugh That's an interesting convention. Could you point me in the way of documentation for it. Also, can you give an example of a correct code usage of what you just mentioned. Thank you for the explanation! – ycx Dec 17 '18 at 15:42
  • 1
    I think [this question](https://stackoverflow.com/questions/5893163/what-is-the-purpose-of-the-single-underscore-variable-in-python) covers all the bases. The general use cases are things like `foo, _ = returns_tuple()` and `[[] for _ in range(5)]` – Patrick Haugh Dec 17 '18 at 15:46
  • Thanks Patrick! I learnt something new myself too. I'll edit the answer to address this concern. – ycx Dec 17 '18 at 15:49
0

Another option is to build a translation table then use str.translate to discard the vowels:

remove_vowels = str.maketrans('', '', 'aeiou')

'example text'.translate(remove_vowels)
# 'xmpl txt'
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
0

Welcome to SO!

Everytime an item is popped from new, the list gets shorter, thus leading to an index error. You could instead do:

Edit: like @Rarblack pointed out, this does not work on strings with more than one vowel directly in a row. Sorry for that.

for i in new:
    if i in ('a', 'e', 'i', 'o', 'u'):
        new.remove(i)

str1 = ''.join(new)
Flob
  • 898
  • 1
  • 5
  • 14
  • damn, you're right... I'm sorry, tested it with the wrong example. This will only work on strings that have no more than one vowel in a row. – Flob Dec 20 '18 at 09:21
0

You can use str.translate or a regular expression, i.e.

new.translate(str.maketrans('','','aeiou'))

where str is the string keyword and not your variable name, or you could do

import re
new = re.sub('[aeiou]','',input())
sehigle
  • 322
  • 1
  • 6