Jump to content

Python list operation

Wictorian

Is  there an easy way to add g and that letter after every vowel? ( I don't have to do this with lists, just basically do that operation on a word but I thought it would be suitable to use a list)

example:

input = [i,n,p,u,t]

output = [i,g,i,p,u,g,u,t]

 

Link to comment
Share on other sites

Link to post
Share on other sites

Not sure how to do this in Python exactly, but it should be as easy as iterating over your list, and then adding either the element or the element + g + element into a new list based on a condition.

 

Some pseudo code

for (element in source) {
    if (element == 'a' || element == 'e' || …) {
        output.add(element)
        output.add('g')
    }
    output.add(element)
}

 

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

of course, you can use list comprehensions to do it in a single line (two if you count the function definition):

def dothethingtothestring(input):
  return ''.join([letter + "g" + letter if letter in "aeiou" else letter for letter in input])

sample output:

image.png.5ab43e5481bb5c20bde05b127713c31c.png

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Sauron said:

of course, you can use list comprehensions to do it in a single line (two if you count the function definition):


def dothethingtothestring(input):
  return ''.join([letter + "g" + letter if letter in "aeiou" else letter for letter in input])

sample output:

image.png.5ab43e5481bb5c20bde05b127713c31c.png

Thanks. This was what I was asking for but still the previous one did the job.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×