One of the standout features of Python is its powerful collection of built-in data structures, which can be classified as either mutable or immutable. Lists, Sets and Dictionaries are mutable, meaning their contents can be altered after creation. Tuples and Strings are immutable, i.e., initial values given to them during creation cannot be changed. In today’s post, let’s take a deep dive into world of Python Lists.

How do we create an empty list in Python?

Firstly, we need to associate a name with the list we wish to create. This name should be a legal identifier (only letters, underscore and digits are allowed; can start either with a letter or an underscore; cannot contain white-space; cannot be a keyword). Next, we use the **list **keyword followed by a pair of round brackets as shown below:

my_list = list()

Alternatively, we can use a pair of empty square brackets as shown below:

my_list = []

In both scenarios, we get an empty list identified by my_list

How do we create a list with values?

Mind you, lists are heterogeneous. They can contain different types of values or elements which are all of the same type as shown below:

my_list_1 = [1, 2, 3, 4]
my_list_2 = [3.14, 2.73, 1729, 28]
my_list_3 = [3.14, 2.73, "pi", 22, 7, "exponent"]

#lists-in-python #python-programming #programming

Python Lists I and How do we create an empty list in Python?
1.35 GEEK