List indices must be integers, not str


#1

https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/2

Hey guys, this is my code:

def anti_vowel(text):
l = list(text)
for i in l:
if i in "aeiouAEIOU":
del(l[i])
return l

and I keep getting this error message:

Oops, try again. Does your anti_vowel function take exactly one argument (a string)? Your code threw a "list indices must be integers, not str" error.

I am pretty sure, that list indices can be strings too. Am I wrong? what's the matter with this problem?


8/15 Anti_Vowel - Working code evaluates as false
#2

You are returning a list when it should be a string.

    return "".join(l)

#3

Not sure the parens are needed or valid here.

del l[i]

is the form I've seen.


#4

Well, it doesn't seem to be the solution.

When I change my code to:

def anti_vowel(text):
l = list(text)
for i in l:
if i in "aeiouAEIOU":
del l[i]
return "".join(l)

I still get the same error maessage.

P.S.: how can I copy the code with indents?


#5

To post code samples, prefix and postfix the code with

```

// your code goes here

```

.

The back-tick character is usually on the same key as the tilde ~. North American keyboards have this next to the 1.

I left the int error for your to discover, but maybe should have just shown you...

for i in l:

i will be a character. When we go to refer to it as an index, we get the int error since subscripts must be integers, not letters.

for i in range(len(l)):

Now i will be integer.


#6

Ok, thx
I now got this error Message fixed but have a few new xD

def anti_vowel(text):
    l = list(text)
    for i in range(len(l)):
        if i in "aeiouAEIOU":
            del l[i]    
    return "".join(l)

now I understand why there was this int/str problem and I wonder if it is possible to definde this function by having i as an integer.
How can I check with an integer for strings?

i tried


#7

i tried

if i in range(len("aeiouAEIOU")):

if i == "a" or "A" or...

neither of it worked. I think I understand why it doesnt work, but how can I define this function by having i as an integer? Is it possible?


#8

When we use range(), i is an integer by default. It is the index of the list.

if l[i] in "aeiouAEIOU":

#9

ok, i now have it that way:

def anti_vowel(text):
    l = list(text)
    for i in range(len(l)):
        if l[i] in "aeiouAEIOU":
            del l[i]    
    return "".join(l)

still error message:
Your code looks a bit off--it threw a "list index out of range" error. Check the Hint if you need help!

I have found a correct code:

def anti_vowel(text):
    l = []
    for i in text:
        if i not in "aeiouAEIOU":
            l.append(i)
        a = "".join(l)
    return a

I totally understand why the second code works, i just dont understand why mine is still wrong


#10

It has to do with deleting elements from an object we are iterating. The range value is set at the start of the loop, and remains fixed. Once elements are deleted, the list gets shorter but the range doesn't change. I think we touched on this a little earlier (as in bad practice).


#11

So, i can only create this function by adding the not-vowel words to a new list instead of deleting them from the old one, right?


#12

It's safest to look for ways that do not destory anything in the process. A secondary list is the best option in this case, imho. We only need to glance at the main list.


#13

I just looked through the solutions in the forum and I only found mehtods of creating new lists without the vowels. The porblems of my function are now clear to me, but is it just the best option to create a new list, or is it the only option?

Is there a solution by deleting letters of the list?


#14

Only in a cleverly constructed loop construct. But what is the use? We do not need to change the list in-place, only produce a corresponding list that is accurate.


#15

Ok, thank you very much =)