0

This question is rather "conceptual" than a specific programming one.

If you ask the type of this object in Python:

[4,3,2,1]

Python will tell you it is a list, as can be seen by doing:

type([4,2,3])
Out[12]: list

In general, the brackets seem to have this effect on a group of objects, whenever you surround them with brackets, they become a list.

Now consider a pandas dataframe. It is known that to get to know the names of the columns you should do, for instance:

list(iris)
Out[13]: ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species']

Which will give you the columns names as a list.

Now if I try:

[iris]

Python returns the whole iris dataframe.

Why is that? Why sometimes the brackets behave as if creating a list out of a group of objects and sometimes not (as in the second case of this example)?

I tried searching Python foruns for this question but was unable to find an answer that I could either understand or be satisfied with.

Best regards,

Gustavo,

0

1 Answer 1

3

Because the list built-in is a function, that creates a new list. When this function receives an iterable as first parameter as in your example, the list will contain all the members of the iterable.

Using square brackets also creates a new list, but with the elements between the square brackets a items.

So calling list(iterable) results in a list containing each element of the iterable while [iterable] results in a list containing the whole iterable itself.

Example:

>>> iterable = range(10)
>>> list(iterable)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> [iterable]
[range(0, 10)]

For more information about to different ways to create a list, take a look at the documentation for pythons list class

Sign up to request clarification or add additional context in comments.

5 Comments

Hi kalehmann thanks for the quick answer. What do you mean by "its first argument"?. It seems to me that every operator has to work on some "content". I got one answer but it raised another one (the classic learning process :) ). Now I need to understand the difference between "first argument" and "its content". Best regards.
You're welcome!
Sorry for my late edit. I pressed enter before finishing writing my whole comment. Anyhow, you will be even more welcome if you elaborate on my new comment about your first answer. Best regards, Gustavo.
I have updated my answer, feel to mark it as accepted if it answers your questions
If think I got the point. At first site they give you the same answer but with slightly different methods which in the end may have different results. The function "list" gets the arguments and if they are iterable it iterates over them and produce several itens, one per iterable as a result. The brackets on the other hand creates a list with whatever you give to it, be it iterable or not. Thanks a lot for your help. It helped me clarifying my understanding of Python! Best regards. PS: As soon as I have earned my 15 points I will upvote your answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.