Confused about .join()

Confused about .join()



I’m new to python and completely confused,



why ','.join('a','b','c')rised an error, a ",".join(['a','b','c'])didn’t.


','.join('a','b','c')


",".join(['a','b','c'])



Why the code below have the same output?


In [3]: ",".join('a':1,'b':2,'c':3)
Out[3]: 'b,a,c'

In [4]: ",".join('a':2,'b':1,'c':3)
Out[4]: 'b,a,c'

In [5]: ",".join('a':3,'b':2,'c':1)
Out[5]: 'b,a,c'





Perhaps you meant to put: ','.join(('a', 'b', 'c')) for your first join example which would be valid.
– roryrjb
Aug 25 at 14:16


','.join(('a', 'b', 'c'))




3 Answers
3



join takes only one argument of the iterable type. ','.join('a','b','c')gives join 3 arguments of type string instead of 1. Whereas ",".join(['a','b','c']) gives join only 1 argument of type list which is an iterable. Single or double quotes make no difference.


join


','.join('a','b','c')


",".join(['a','b','c'])



For your second question, all 3 dictionaries give the same output since what value is stored for a key makes no difference. So whether a key has a value of 1 or 3 or perhaps some string, it wont affect the order. The order of a dictionary is inherently arbitrary. It cannot be predicted in advance and it is because of the fact how dictionaries are stored in memory.



The docs on str.join state that the method takes an iterable which is any type that can be iterated over (list, tuple, etc.):


str.join


iterable



str.join(iterable)¶


str.join(iterable)¶



Return a string which is the concatenation of the strings in iterable. A TypeError will be raised if there are any non-string values in iterable, including bytes objects. The separator between elements is the string providing this method.


TypeError



In your first example, you pass three arguments to a method which expects one so receive a TypeError, whereas you use the method as intended in the example where you pass a list (indicated so through the use of square brackets). In your typed examples, you are passing dictionaries which are also iterable, however, when iterated over, they yield their keys (note: the order is undetermined). So the keys of say: 'a':1,'b':2,'c':3 are: 'a', 'b' and 'c' and since these are all strings, the method works fine to join them together with a comma


TypeError


'a':1,'b':2,'c':3


'a'


'b'


'c'





@user18861 since your new to Python here's more on iterables: docs.python.org/3/tutorial/classes.html#iterators.
– roryrjb
Aug 25 at 14:17



Because with ','.join('a','b','c'), you're providing 3 arguments, each of which is a str, to str.join(), which is unexpected (expected 1).


','.join('a','b','c')


str


str.join()



With ','.join(['a','b','c']), you're providing only 1 argument which is a list, and it works well.


','.join(['a','b','c'])


list






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

How do I collapse sections of code in Visual Studio Code for Windows?