1

For the robber's language challenge thing Wikipedia I struggled with my limited knowledge and came up with this:

def translate(txt):
    new_txt = ''
    consonants = 'bcdfghjklmnpqrstvwxz'
    for x in txt:
        if x in consonants:
            new_txt += x + 'o' + x
        else:
            new_txt += x
    return new_txt
print(translate("Hello World"))

I understand the += operator works to append the new_txt variable, versus using just =, which would result in an answer of just "dod" in this example as it replaces everything each cycle of the for loop leaving the last run when x == d. I found several pithier solutions to the problem, however my experience with both Python and coding in general is lacking, and I can't understand several aspects of these answers. For example:

def translate(txt):
    consonants = 'bcdfghjklmnpqrstvwxz'
    new_txt = (x + 'o' + x if x in consonants else x for x in txt)          
    return ''.join(new_txt)
print(translate("Hello World"))

which is provided with varying levels of similarity in a few threads by several users, however I don't understand how this

new_txt = (x + 'o' + x if x in consonants else x for x in txt)
return ''.join(new_txt)

part differs from what I came up with. What type of component is this, and how does it do the job of the += on the new_txt string and append the result of each subsequent cycle of the

new_txt = (x + 'o' + x if x in consonants else x for x in txt) 

line? What gives the result of that statement persistence, i.e. why isn't it replaced for each cycle and character of the entered txt string? What is this concept called? I have looked online and in StackOverflow, but it's difficult because I don't know what I'm looking for as someone very new to coding. I have a feeling that it's something very fundamental and I should know the answer in order to attempt something like this, however at this point, if I don't ask I won't know. Thank you.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 3
    [Explanation of how list comprehension works?](https://stackoverflow.com/questions/20639180/explanation-of-how-list-comprehension-works) -- what you have is a genexp, but the differences aren't very pertinent. Similarly: [What does "list comprehension" mean? How does it work and how can I use it?](https://stackoverflow.com/questions/34835951/what-does-list-comprehension-mean-how-does-it-work-and-how-can-i-use-it/34932520) -- and to explain how those explanations relate to what you have here, https://stackoverflow.com/questions/43943573 – Charles Duffy Mar 31 '18 at 19:44
  • The expression in parentheses is a "generator expression". If you replaced the parentheses with square brackets it would be a "list comprehension". Either way, it generates an iterable sequence of strings (e.g., "HoH", "e", "lol", ...). Then the `"".join()` expression combines all of them into a single string (lookup `str.join` for more details on this). – Matthias Fripp Mar 31 '18 at 19:51
  • when passing to join, it's better to use a list comprehension, since join needs to build a list. It's faster that way. – Jean-François Fabre Mar 31 '18 at 19:55
  • Thanks for your help and the resources/links provided. I wasn't aware of this useful functionality in Python. – NotThisAgain Apr 01 '18 at 11:30

1 Answers1

0

This looks similar to list comprehension, which would be the same, but using []-brackets. The above code as list comprehension would be:

new_txt = [x + 'o' + x if x in consonants else x for x in txt]
return ''.join(new_txt)

List comprehensions (and dictionary, set comprehensions) are a pythonic way to replace loops. So instead of writing one or more loops, you can use a one-liner as the above mentioned list comprehension. It's also used to replace functions like map(), reduce() and filter(). The example can be rewritten using for-loops:

new_txt = []
for x in txt:
    if x in consonants:
        new_text += x + 'o' + x
    else:
        new_text += x
return ''.join(new_text)

The join method concatenates every element of the list with the delimiter specified before, which is in this case an empty string, so basically no delimiter.

List comprehensions are generally structured as follows:

[x for x in iterable]

You can add conditionals to it as well. This was done by the example you provided to us.

For further information have a look at this great tutorial from DataCamp.

Hope it helped you. Sincerely.

Train
  • 585
  • 1
  • 6
  • 20