-3

I started learning python and while trying some exercises on codewars it stumbled upon the following solution:

pairs = {'A':'T','T':'A','C':'G','G':'C'} def DNA_strand(dna): return ''.join([pairs[x] for x in dna])

Now what exactly does return ''.join do?

Alessio C.
  • 87
  • 1
  • 1
  • 7
  • `''.join([pairs[x] for x in dna])` is an expression. It is evaluated, and the result is the return value from the function. That's how a `return` statement works. As for the string method `join`, it creates a string from the elements of its argument, which is an iterable, separating them with the given string (in this case, an empty string). – Tom Karzes May 07 '20 at 15:09
  • Sorry my quiestion is actually what the quotation marks do – Alessio C. May 07 '20 at 15:14
  • 1
    `''` is an empty string constant. Type it into your interpreter and see. It's the same as `""`. `'abc'` is a string constant with 3 characters, `'a'` is a string constant with 1 character, and `''` is a string constant with 0 characters. This is called an empty string. – Tom Karzes May 07 '20 at 15:15
  • ooh, alright. Thanks! – Alessio C. May 07 '20 at 15:17
  • 1
    The [official docs](https://docs.python.org/3/library/stdtypes.html#str.join) are always a good place to start with this kind of doubts – Tomerikoo May 07 '20 at 15:18

1 Answers1

1

It makes a string out of the list. The empty string ('') says that the string between each list element is nothing.

For example:

If dna = ['A', 'T'] (keys) it returns the corresponding values T and A as the string "TA".

In this example: ':'.join(...) would return the string "T:A"

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Simon Rechermann
  • 467
  • 3
  • 18