Python Append to Empty List
In Python, we can create an empty list and then later append items to it, using the Python append() function.
Example:
In this example, we will first create an empty list in Python using the square brackets and then appended the items to it.
Python3
# create an empty list a = [] # append items to it a.append( 10 ) a.append( 20 ) a.append( 30 ) print ( "Values of a:" , a) print ( "Type of a:" , type (a)) print ( "Size of a:" , len (a)) |
Output:
Values of a: [10, 20, 30] Type of a: <class 'list'> Size of a: 3
Declare an empty List in Python
Lists are just like arrays, declared in other languages. Lists need not be homogeneous always which makes it the most powerful tool in Python. A single list may contain data types like Integers, Strings, as well as Objects. Lists are mutable, and hence, they can be altered even after their creation.