Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by

This actually confused me a lot. The below code is better:

my_list = ["Hello", "world"]

print(my_list.join("-"))

# Produce: "Hello-world"

than:

my_list = ["Hello", "world"]

print("-".join(my_list))

# Produce: "Hello-world"

I just want to know, whether is there any reason for being like this?

closed

4 Answers

0 votes
by (25.7k points)
 
Best answer
In the given code examples, there seems to be a misunderstanding.

The correct usage of the join() method in Python is as follows:

my_list = ["Hello", "world"]

print("-".join(my_list))

The join() method is called on the delimiter ("-" in this case), and the list you want to join is passed as an argument. It concatenates the elements of the list using the specified delimiter.

The incorrect code example you provided, print(my_list.join("-")), won't work because the join() method is a string method, not a list method. It should be used on a string to join a list of strings together, rather than being used directly on the list itself.

So, the correct and recommended approach is to use "-" as the delimiter and call join() on it, passing the list as an argument, as shown in the corrected code example.

To summarize, the reason for the correct usage being "-".join(my_list) is that the join() method is designed to be used on a string object and accepts an iterable (like a list) to join its elements together using the specified delimiter.
0 votes
by (26.4k points)

This is because that any iterable can be combined (e.g, list, tuple, dict, set), however, its substance and the "joiner" should be strings

Example:

'_'.join(['welcome', 'to', 'stack', 'overflow'])

'_'.join(('welcome', 'to', 'stack', 'overflow'))

'welcome_to_stack_overflow'

When you try to use other than string, it will raise the accompanying error:

TypeError: sequence item 0: expected str instance, int found

Interested to learn the concepts of Python in detail? Come and join the python course to gain more knowledge in Python

Watch this video tutorial, if you want to gain more information

0 votes
by (15.4k points)
The reason for using "-" as the delimiter and calling join() on it, passing the list as an argument ("-".join(my_list)), is because the join() method is designed to be used on a string object. It concatenates the elements of the iterable (in this case, a list) using the specified delimiter.

On the other hand, my_list.join("-") won't work as intended because the join() method is not a list method. It is a string method that operates on a string object and requires an iterable to join its elements.

To achieve the desired result of joining the elements of the list with a delimiter in between, it is necessary to call join() on the delimiter itself, passing the list as an argument.

Therefore, the recommended and correct approach is "-".join(my_list).
0 votes
by (19k points)
The reason for using "-".join(my_list) is because the join() method is specifically designed as a string method that concatenates the elements of an iterable (such as a list) using the specified delimiter.

On the other hand, my_list.join("-") does not work as expected because the join() method is not available as a list method. It is only accessible as a string method.

To correctly join the elements of the list with a delimiter in between, you need to call the join() method on the delimiter string and provide the list as an argument. This allows the method to concatenate the elements of the list with the specified delimiter.

In summary, using "-".join(my_list) is the recommended and accurate way to achieve the desired result of joining the elements of the list with a specific delimiter.

Related questions

0 votes
1 answer
0 votes
4 answers
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...