Python list() function | applied electronics engineering


Python list() function

By Applied Electronics - Saturday, April 8, 2017 No Comments
The python list() function is one of construct that allows you to group data types. Other construct that are also used to group data types are the dictionary and array. In python programming language the list() function is actually an object of data type list. In python everything is object.

The python list() function is a very useful and powerful construct. It is used to group different data types or group similar data types. It is used to perform iterative action on those data in the list. It is used in for loops.

The python list() function is similar to array but there are also differences. Like array list is used to store elements. But unlike array, list can be used to store different data types like integers, floats, string, character and even another list or dictionary. Also list allows one to add, delete items in the list. But using list also has disadvantages. Computation with list is slower than array. Array also requires less memory space than list. Vectorization is not possible with list.

In the earlier tutorial The if, for and while statement in Python programming language we have used list in a for loop. Also there we used range() function which returns a list. For example range(3) gives [0,1,2,3] which is a list. We can convert the list to an array in python. Suppose L is a list then to convert to array we can use array(L).

Some examples of using list are now given.

As said earlier a list can contain different data types objects. Below is an example of such.

L = [’hello’, 4, 3.14]

Then indexing is used to refer to the list elements. So suppose you want to work with the hello element then you would refer to it in the following way.


Similarly, L[1] = 4, L[2] = 3.14

Suppose you want to insert an element 99 at the beginning with index 0. Then you would use the insert() function as follows.

L.insert(0, 99)

L is then [99, ’hello’, 4, 3.14]

To delete the 3rd element 4 you would write,

del L[2]

Then L is [99, 'hello', 3.14]

The final function often used with list the append() function. To append some data object into the existing list we do the following.


Then the updated list is [99, 'hello', 3.14, 'john']

 Another place where list is used often is in for loop construction as said earlier. Following is an example of using list in for loop in python programming language.

x = [’hello’, 4, 3.14, 6]
for k in x:
      print(’x element: ’,k)


No Comment to " Python list() function "