How to use the Python builtin function list() to generate lists

There are many builtin functions which come by default with Python, utilities that the coder can use to accomplish many different tasks in their real world projects. Although most of them are being explained in details in the Python’s official documentation, the beginner finds it really hard to truly understand most of them. Taking this fact in consideration, I am going to share some information on how one can make use of the Python builtin function called list() to generate list objects.

Based on my personal experience with the Python computer programming language, the builtin list() function can be easily used to generate list objects. The following piece of code, creates a new list object, once it is being executed in the Python interpreter.

l = list()

The result of the above code is shown below.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

l
[]

The Python builtin function called list can be also used to generate a list object by taking a tuple as an input like shown in the following example.

t = (1, 2, 3)
l = list(t)

To check the content to which the variable l links to, after the above code gets executed, just run the following statement in the interactive shell.

l

And the following list object will get displayed on the interactive console.

[1, 2, 3]

According to the official documentation about the Python computer programming language, any iterable object can be converted into a Python list by making use of the builtin function called list().

Let’s try to convert a string object into a list one by making use of the Python builtin function called list().

First, make sure to declare the string with the syntax which is being shown in the following example.

s = ‘list’

Then make use of the Python builtin utility list() by passing the string as an input like shown in the example below.

string_to_list_obj = list(s)
print(string_to_list_obj)

Once the above piece of Python code gets executed in the Python interactive shell, the following result gets displayed on the console.

[‘l’, ‘i’, ‘s’, ‘t’]

Not only can the Python coder make use of the builtin utility list() to generate list objects from strings and tuples, but also from dictionaries. Based on my personal experience, when a dictionary is passed as an input to the builtin utility, a list with its keys as elements gets generated.

To illustrate and make things more practical, let’s define a dictionary object in the Python interactive shell like shown below.

d = {‘website’: ‘codetheory’, ‘domain’: ‘codetheory.in’, ‘status’: ‘live’}

Then with the help of the Python builtin utility called list(), try to generate a list from the above dictionary object by following the piece of code shown in the following example.

list_out_of_dict = list(d)

After the above piece of Python code is being executed in the interactive console, run the following statement to get the content of the list being created from the dictionary object.

list_out_of_dict
[‘website’, ‘status’, ‘domain’]

Final thoughts

The list object is very important for the Python coder, especially when they want to store data in them which changes in time. A very useful utility which can be used to generate list objects from different Python iterables is the list() builtin.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Leave a Reply

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