Creating an empty list in Python

0 votes

What is the best way to create a new empty list in Python?
l = []
or
l = list()
I am asking this because of two reasons:​

  1. Technical reasons, as to which is faster. (creating a class causes overhead?)​

  2. Code readability - which one is the standard convention.

Aug 17, 2018 in Python by bug_seeker
• 15,350 points
72 views

1 answer to this question.

0 votes

Here is how you can test which piece of code is faster:

% python -mtimeit "l=[]" 10000000 loops, best of 3: 0.0711 usec per loop % python -mtimeit "l=list()" 1000000 loops, best of 3: 0.297 usec per loop

However, in practice, this initialization is most likely an extremely small part of your program, so worrying about this is probably wrong-headed.

Readability is very subjective. I prefer [], but some very knowledgable people, like Alex Martelli, prefer list() because it is pronounceable.

answered Aug 17, 2018 by Priyaj
• 56,900 points

Related Questions In Python

0 votes
1 answer

Create an empty list in python with certain size

Try this instead: lst = [None] * 10 The ...READ MORE

answered Aug 2, 2018 in Python by bug_seeker
• 15,350 points
418 views
+1 vote
4 answers

Count the frequency of an item in a python list

To count the number of appearances: from collections ...READ MORE

answered Oct 18, 2018 in Python by tinitales
1,073 views
0 votes
1 answer

Empty a list in python.

You could use the clear() method of list which is parallel ...READ MORE

answered Dec 17, 2018 in Python by Shuvodip
53 views
0 votes
1 answer

How to check if a list is empty in python?

To check if a list is empty ...READ MORE

answered May 27 in Python by Nisa
• 1,090 points
43 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 282 views
+4 votes
6 answers
+2 votes
3 answers

How can I play an audio file in the background using Python?

down voteacceptedFor windows: you could use  winsound.SND_ASYNC to play them ...READ MORE

answered Apr 3, 2018 in Python by charlie_brown
• 7,720 points
1,931 views
+1 vote
1 answer

How to print an error in Python?

For Python 2.6 and later and Python ...READ MORE

answered Aug 23, 2018 in Python by Priyaj
• 56,900 points
208 views