Methods and Functions

Python list ()

The list() python is a built-in function that works as a constructor. Lists are data structures in Python that are defined as dynamic arrays. These store similar types of data types such as integers, strings, objects, etc. They are mutable and ordered, with each element recognized with a unique index.

list() python

Source

Lists are popularly used to save any size of data in sequence such that it can be iterated and manipulated as required.

It is used to create a list object. A list object refers to the collection of data that is ordered and changeable. To understand the working of the list () function, one must understand how lists are used in Python along with different data types. Also, note that the parameters that are passed in the list () function are considered as objects.

List () Parameters

The syntax followed to apply the function of the list () Python is as provided below.

list (iterable)

Here, iterable refers to the sequence of collection of data. It can also be an iterator object. Note that it is an optional parameter. Thus, if no parameter is passed in the list () function, then the compiler will not show an error in the code.

The object that is passed as the iterable can be in the form of a sequence such as string or tuple or a collection such as a set or a dictionary.

Return Value from the list ()

If there are no parameters in the list () Python function, then it will return an empty list. However, if any specific and acceptable parameter is passed, then the function will create a list that consists of the items present in the iterable.  

Example 1: Create lists from string, tuple, and lists

The program given below will create an empty list, a list through the string, one that has a tuple in its parameter, and the last one that includes a list.

                    

print (list ())

even = “246810”

print (list (even))

even1 = (“2”, “4”, “6”, “8”, “10”)

print (list (even1))

even2 = [“2”, “4”, “6”, “8”, “10”]

print (list (even2))

The output for all the lists that have parameters will be the same. Thus, using the list () function, one can create a list using any type of sequence.

Example 2: Create lists from set and dictionary

The list () function is also used to create a list using a collection. The program below shows how lists can be created through sets and dictionaries.

                    

even = {“2”, “4”, “6”, “8”, “10”}

print (list (even))

even1 = {“2”: 1, “4”: 2, “6”: 3, “8”: 4, “10”: 5}

print (list (even1))

The output of the program will be the same as before. However, when dictionaries are used as the parameters for the list () Python function, then you need to note that the keys attributed to each value stored in the dictionary will also be items of the list. Also, the order of the elements that are stored in the list after being converted from a dictionary will be random.   

Example 3: Create a list from an iterator object

The program below showcases how an iterator object can be used to create a list using the list () function.

                    

class Cubes:

              def __init__ (self, max):

                             self.max = max

              def __iter__ (self):

                             self.num = 0

                             return self

              def __next__ (self):

                             if (self.num >=self.max):

                                            raise StopIteration

                             return = 3 **self.num

                             self.num += 1

                             return result

cubes = Cubes (6)

cubes_iter = iter (cubes)

print (list (cubes_iter))

The program will print the cubes of the first n natural numbers. The value of n is input by the user and the results are printed in the form of a list that contains values of cubes of the natural numbers as its elements.  

Frequently Asked Questions

What are list operations in Python?

There are various list operations available in Python that can be used to manipulate and modify the data stored in the list. These operations include append (), extend (), and insert () that are used to add an element to the list. Others are remove (), pop (), len (), reverse (), etc.

What is a list in Python?

A list is a data structure in Python that is used to store similar types of data in a sequence. Lists are ordered and mutable data types. They are dynamic, that is, their length and value can be changed after their creation as well.

How do you create a list in Python?

To create a list in Python, square brackets are used. The elements that are present inside these [] brackets are considered as elements of the list, each separated by a comma.  

How do I show a list in Python?

To show a list in Python, all the elements can be printed. FOR loop can be used to iterate over each element present in the list. Using the print () function with the name of the list as the parameter can be the easiest method to show a list in Python.   

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.