vowel finder, error: list index out of range

  • Last Update :
  • Techknowledgy :

You can try something like this:

vowels = 'aeiou'
string = input('Input string > ')
vow_in_str = []

for char in string:
   if char in vowels:
   vow_in_str.append(char)

print(vow_in_str)

Here's a much faster one:

z = ['a', 'e', 'i', 'o', 'u']

n = input('Input string: ')

m = [x
   for x in n
   if x in z
]

print(m)

No need for that double loop, they take too long once you get into bigger lists.

>>> Input string: hello >>>
   ['e', 'o']

The code can be modified as follow:

  for i in z:
     for j in a:
     if i == j: #
  try to match letter by letter
  print('vowel found')
  m.append(i)
  else:
     continue

st = "Nice day"

z = ['a', 'e', 'i', 'o', 'u']
# convert st to list of chars
y = [ch.lower() for ch in st
   if ch != ' '
]

# convert both list to sets and find the lenght of intersection
print(len(set(z) & set(y)))

3

Suggestion : 2

I suggest you ignore lists and instead do something like this: result = "" and you add the elements, if they’re not a vowel.,def anti_vowel(text): an_vo = “” for i in text: if i not in “aeiouAEIOU”: an_vo += i return an_vo,As for what happens in your case (I replaced range with its result for demonstration purposes), here’s an analoguous example:,It returns a “String index out of range” error. Can someone help me out ?

This is my code for the anti-vowel problem :

def anti_vowel(string):
   my_list = list(string)
for i in range(0, len(my_list) - 1):
   if my_list[i] in "aeiouAEIOU":
   my_list = my_list.pop(i)
return my_list

As for what happens in your case (I replaced range with its result for demonstration purposes), here’s an analoguous example:

my_list = ['a', 'b', 'c']
for i in [0, 1, 2]:
   my_list = my_list.pop(i)

in the first iteration, i = 0
my_list = my_list.pop(0)
   -- > my_list == ['b', 'c']

   -- > next iteration, i = 1
my_list = my_list.pop(1)
   -- > my_list == ['b']

   -- > next iteration, i = 2
my_list = my_list.pop(2)
   -- > error, there 's no element with index 2!

I suggest you ignore lists and instead do something like this: result = "" and you add the elements, if they’re not a vowel.

def anti_vowel(string):
   result = ""
for c in string:
   if c.lower() not in "aeiou":
   result += c
return result

Suggestion : 3

The index() method returns the index of the specified element in the tuple.,In the above example, we have used the index() method to find the index of a specified element in the vowels tuple.,The element 'e' appears in index 1 in the vowels tuple. Hence, the method returns 1.,In the above example, we have used the index() method to find the index of an element that is not present in the numbers tuple.

Example

# tuple containing vowels
vowels = ('a', 'e', 'i', 'o', 'u')

# index of 'e' in vowels
index = vowels.index('e')

print(index)

# Output: 1

Example

# tuple containing vowels
vowels = ('a', 'e', 'i', 'o', 'u')

# index of 'e' in vowels
index = vowels.index('e')

print(index)

# Output: 1

The syntax of the index() method is:

tuple.index(element, start_index, end_index)

Example 1: Python Tuple index()

# tuple containing vowels
vowels = ('a', 'e', 'i', 'o', 'i', 'u')

# index of 'e' in vowels
index = vowels.index('e')

print('Index of e:', index)

# index of the first 'i'
is returned
index = vowels.index('i')

print('Index of i:', index)

Example 2: index() throws an error if the specified element is absent in the Tuple

# tuple containing numbers
numbers = (0, 2, 4, 6, 8, 10)

# throws error since 3 is absent in the tuple
index = numbers.index(3)

print('Index of 3:', index)

Example 3: index() With Start and End Parameters

# alphabets tuple
alphabets = ('a', 'e', 'i', 'o', 'g', 'l', 'i', 'u')

# returns the index of first 'i' in alphabets
index = alphabets.index('i')

print('Index of i in alphabets:', index)

# scans 'i'
from index 4 to 7 and returns its index
index = alphabets.index('i', 4, 7)

print('Index of i in alphabets from index 4 to 7:', index)

Suggestion : 4

That won’t work. It causes the runtime error IndexError: list index out of range. The reason is that len(seq) returns the number of elements in the list, 16, but there is no element at index position 16 in seq.,You probably expected to see 'b', but computer scientists typically start counting from zero, not one. Think of the index as the numbers on a ruler measuring how many elements you have moved into the sequence from the beginning. Both rulers and indices start at 0.,Using slices to delete list elements can be awkward, and therefore error-prone. Python provides an alternative that is more readable.,A data type that contains a sequence of elements of any type, like a list, but is immutable. Tuples can be used wherever an immutable type is required, such as a key in a dictionary (see next chapter).

[10, 20, 30, 40, 50]
["spam", "bungee", "swallow"]
(2, 4, 6, 8)
("two", "four", "six", "eight")[("cheese", "queso"), ("red", "rojo"), ("school", "escuela")]
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
   >>> thing
   (2, 4, 6, 8)
>>> singleton = (2,)
>>> type(singleton)
<class 'tuple'>
   >>> not_tuple = (2)
   >>> type(not_tuple)
   <class 'int'>
      >>> empty_tuple = ()
      >>> type(empty_tuple)
      <class 'tuple'>
>>> fruit = "banana" >>>
   fruit[1]
'a' >>>
fruits = ['apples', 'cherries', 'pears'] >>>
   fruits[0]
'apples' >>>
prices = (3.99, 6.00, 10.00, 5.25) >>>
   prices[3]
5.25
   >>>
   pairs = [('cheese', 'queso'), ('red', 'rojo'), ('school', 'escuela')] >>>
   pairs[2]
   ('school', 'escuela')
>>> len('banana')
6
>>> len(['a', 'b', 'c', 'd'])
4
   >>>
   len((2, 4, 6, 8, 10, 12))
6
   >>>
   pairs = [('cheese', 'queso'), ('red', 'rojo'), ('school', 'escuela')] >>>
   len(pairs)
3