How to create an empty list in python

In this post, we will see how to create an empty list in python.

There are two ways to create an empty list in Python.

  • Using [ ]
  • Using list() function.

Let’s see this with the help of example.

Output:

List 1: [1, 2] List 2: [1, 2]

Which is faster: [ ] or list()

list() is inherently slower than [], because of

  • symbol lookup (no way for python to know in advance if you did not just redefine list to be something else!)
  • function invocation
  • then it has to check if there was iterable argument passed (so it can create list with elements from it)

That’s all about creating empty list in python.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 29 November

    Prefix b Before String in Python

    Table of ContentsPrefix b Before String in PythonConclusion Prefix b Before String in Python Prefix b before String denotes a byte String. By putting b before String, you can convert String to bytes in Python. The upgrade from Python 2 to Python 3 was considered a major change as many new features were introduced and […]

  • 29 November

    How to Log to Stdout in Python

    Table of ContentsUse logging.basicConfig() to log to stdoutUse logging.StreamHandler() to log to stdout Use logging.basicConfig() to log to stdout To log to stdout in Python: Specify the format in which we want to have all logs. Use the helper function .basicConfig() to perform basic logging for the logging system. Use .getLogger() to create a logger […]

  • 29 November

    Skip Iterations in Python loop

    Table of ContentsSkip iterations in a Python loop.Using the continue statement.Special Case: Using exception handling. 💡TL;DR To skip iterations in Python loop, use continue statement. [crayon-638b4c1695c71652490758/] Looping is one of the fundamentals in Python with one or more types of loops occurring in almost every code. The for loop and the while loop allow the […]

  • 28 November

    Convert String to Path in Python

    Table of ContentsUsing pathlib library [Python 3.4+]Using os.path moduleConclusion Using pathlib library [Python 3.4+] Use Path class’s constructor to convert String to Path in Python. You need to import Path class from pathlib. [crayon-638b4c1695d22186221021/] Output: C:\temp\tempFile.txt We used the Path() constructor to convert String to Path. This constructor accepts a string that contains a required […]

  • 28 November

    Get String Between Two Characters in Python

    Table of ContentsUsing the string-slicing techniqueUsing Regular ExpressionsUsing the split() functionConclusion Using the string-slicing technique To get String between two characters in Python: Use String’s find() method to find indices of both the characters. Use String’s slicing to get String between indices of the two characters. [crayon-638b4c1695dbc334350835/] Output: ava2Blo In the above example, we found […]

  • 28 November

    Check if Date is Greater than Today in Python

    Table of ContentsUse Comparison Operator with now()Compare the datetime of Different Time ZonesUse Comparison Operator with strptime() Use Comparison Operator with now() To check if the specified date is greater than today in Python: Use .datetime.now() to get the current local time and date. Create a datetime object using datetime.datetime() with the specified date and […]

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.