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,520 points
889 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
• 58,090 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,520 points
27,752 views
+1 vote
8 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
35,174 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 18, 2018 in Python by Shuvodip
469 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, 2019 in Python by Nisa
• 1,090 points
711 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,023 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
3,409 views
+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 4, 2018 in Python by charlie_brown
• 7,720 points
12,874 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
• 58,090 points
1,139 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP