4

I just wanted to know the use of the list() constructor because if we want to create a new list then just we can use square brackets, what's the specific use of the list() constructor.

newGrocerylist = list(("apple", "guava", "mango", "pineapple", "orange")) #Why use the list() constructor.
print(newGrocerylist)

#We can also do like this--

newGrocerylist = ["apple", "guava", "mango", "pineapple", "orange"]
print(newGrocerylist)

Why use the list constructor? Isn't it a redundant thing to do--

newGrocerylist = list(("apple", "guava", "mango", "pineapple", "orange"))
Piyush
  • 177
  • 1
  • 8
  • 2
    While this is pretty basic I think it isn't a duplicate of either of the above. I shd check if "What is X used for" a valid question. – user202729 Aug 15 '19 at 13:18
  • 1
    Indeed, the question itself makes clear that OP does know what the list constructor does. They want to know when to use it to do that. This is a different question. Potentially opinionated, but I don't even think so, since there is general agreement on when to use list(). Definitely isn't duplicate. – Neil Aug 15 '19 at 13:33

8 Answers8

11

If you want to create a literal new list with a bunch of new values then you're right. There is no reason to use the list constructor, you should use the literal notation:

my_list = ['a', 'b', 'c']

In fact, it is impossible to create a new list with a bunch of values using the constructor, you can only use it to transform iterables into their list representation:

my_tuple = ('a', 'b', 'c')  # literal notation to create a new tuple
my_list = list(my_tuple)    # this is what you actually did in your first example

You can use the other iterable constructors like set and dict in a similar way. They are not used to create new objects, but transform existing ones into the type they describe.

Arne
  • 17,706
  • 5
  • 83
  • 99
8

Perhaps we want to convert a Map or Set into a list. We would pass it into the constructor.

mylist = list(myset)
Michael Bianconi
  • 5,072
  • 1
  • 10
  • 25
1

list can be passed around as a function object.

So as a toy example you could have a function that creates an arbitrary collection and you could pass in either list or set

def make_collection_from_data(data, collection_maker):
    return collection_maker(data)

data = [1, 2, 3, 4]

make_collection_from_data(data, set) # Returns a list
make_collection_from_data(data, list) # Returns a set
Michael Mauderer
  • 3,777
  • 1
  • 22
  • 49
0

In the initial statement you created a tuple (indicated by round brackets) and converted it with list(). In the second example you directly created a list (with the square brackets).

Carsten
  • 2,765
  • 1
  • 13
  • 28
0

They don't behave the same in all contexts. For example, you can construct a list of dictionary keys, or create a list with one element (one dict).

>>> list({1:2})
[1]
>>> [{1:2}]
[{1: 2}]
Paul M.
  • 10,481
  • 2
  • 9
  • 15
0

To my understanding, list () constructor uses less memory (thus less execution time) than list(). Check the code below and inform me if I am missing something:)

from timeit import default_timer as timer
start = timer()
list1 = ["apple", "banana", "cherry"]
print(list1)
stop =timer()
print(stop-start)

start =timer()
thislist = list(("apple", "banana", "cherry"))
print (thislist)
stop =timer()
print(stop-start)

The results in seconds: List [0.0003924000193364918] and list () constructor[0.00016659998800605536]

  • I'm sorry but what is your answer for the qiestion? – Bohdan Dec 17 '22 at 16:29
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 17 '22 at 16:29
-1

Primarily we use a Constructorlist() when there is a string.

vowel_string = 'aeiou'
print(list(vowel_string))

OUTPUT

['a','e','i','o','u']
-3

Check out: [] and {} vs list() and dict(), which is better?

It looks like the constructor [] or {} for dicts is much faster than list() or dict().

jdpy19
  • 375
  • 1
  • 13