Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialibrahim Warsame
7,803 Pointsthe list() function
What is the diffrence betwen list() and list = [] please answer im confused
3 Answers
Chris Freeman
Treehouse Moderator 68,457 PointsThe notation [ ]
is shorthand for list()
. However, list()
only takes one argument and returns a list. In the trivial case of list()
, the argument is None
and the returned list is the empty list [ ]
.
list()
is not often used to create a list from literal objects such as strings and number since you have to wrap the input in parens or square brackets to form a single object:
["Milk" , "Cheese" , "Low fat cheese", "Beans"] is equivalent to list( ("Milk" , "Cheese" , "Low fat cheese", "Beans") )
At this point it's easier to uses the simple square bracket notation.
The power of list()
comes when evaluating a generator such as range()
or zip()
. These functions create an object that only returns one item at a time. using list()
will iterate over the object and return the results as a list.:
>>> range(5)
range(0, 5)
>>> type(range(5))
<class 'range'>
>>> list(range(5))
[0, 1, 2, 3, 4]
>>> zip([1, 2, 3], ['a', 'b', 'c'])
<zip object at 0x7f133e4924c8>
>>> list(zip([1, 2, 3], ['a', 'b', 'c']))
[(1, 'a'), (2, 'b'), (3, 'c')]
Also list()
does not nest. Running list()
on a list simply returns that same list:
>>> list('a')
['a']
>>> list(list(list('a')))
['a']
To get a list in a list in a list you need to use square brackets:
>>> [[['a']]]
[[['a']]]
More over, if you give list()
a string, it will iterate over the string and produce a list off the characters:
>>> list('tofu')
['t', 'o', 'f', 'u']
Jesse Mikkonen
1,939 PointsI'm not sure, but I think the '''list()''' function is empty first hand, that you fill out by either '''user_input''' or some other method. Where as in ''list[]'' you fill the brackets yourself eg. '''list["Milk" , "Cheese" , "Low fat cheese", "Beans"]''' Please do correct if I'm wrong, starting out myself, too :)
Chris Freeman
Treehouse Moderator 68,457 PointsGlad to see you looking through the forum to help others. Answering forum questions is a good test of what you know so far. I encourage you to research the answers and to compare your solution to what others post.
ibrahim Warsame
7,803 PointsThanks man, I'm new to this too.
Chris Freeman
Treehouse Moderator 68,457 PointsGlad to help. The forum is here to support you. Keep up the good work and keep asking questions!
Jesse Mikkonen
1,939 PointsJesse Mikkonen
1,939 PointsThis clarifies a lot of things for me. Thanks Chris!