0

I'm trying to do something like a "conjugator".

Say I have a list of endings:

endings = ['o', 'es', 'e', 'emos', 'eis', 'em']

and I have a verb root as a string:

root = "com"

The way I thought of doing this is:

for ending in endings:
    print root + ending

which outputs:

como
comes
come
comemos
comeis
comem

But my desired result is:

como, comes, come, comemos, comeis, comem

How can I achieve exactly this (and with no quotes around each of the resulting items, and no comma after the last item)?

  • 1
    Possible duplicate of [Concatenate item in list to strings - python](http://stackoverflow.com/questions/12453580/concatenate-item-in-list-to-strings-python) – joce Nov 22 '15 at 23:39

1 Answers1

6

You need a list comprehension and str.join(). From the documentation:

str.join(iterable)

Return a string which is the concatenation of the strings in the iterable iterable. The separator between elements is the string providing this method.

>>> root = "com"
>>> endings = ['o', 'es', 'e', 'emos', 'eis', 'em']
>>> verbs = [root + ending for ending in endings]
>>> print ", ".join(verbs)
como, comes, come, comemos, comeis, comem
Sebastian Wozny
  • 16,943
  • 7
  • 52
  • 69
  • 1
    one-liner: `print ", ".join(root + ending for ending in endings)` – BMW Nov 22 '15 at 23:54
  • Thanks! I tried using different methods (including those in the question this one is marked as a "possible duplicate" of, but none of them worked; I got either a different output or errors. This works perfect. – Pedro Tiago Martins Nov 23 '15 at 00:24