Python Programming – Creating Lists

You can learn about List Manipulation in Python Programs with Outputs helped you to understand the language better.

Python Programming – Creating Lists

There are several ways to create a new list; the simplest way is to enclose the elements in square brackets [ ], separated by commas. It can have any number of items and they may be of different types (integer, float, string, etc.).

The syntax for creating a list is:
<list_name>          =         [<value>, <value>, <value>]
For example, you can create a list of cities having three items such as Delhi, Mumbai, and Chennai, separated by a comma and enclosed inside square brackets.

City=[ ‘ Delhi ‘ ,  ‘ Mumbai ‘ ,  ‘ Chennai ‘ ]

Other examples of Python lists are:

>>> L1 = [ 10 , 20 , 30 , 40 ] # list of 4 integer elements
>>> L2 = [ ” Raj ” ,  ” Govind ” ,  ” Abdul ” ] # list of 3 strings
>>> L3 = [ 1 ,  1 . 5 ,  2 . 5 , 7 ] # list of integer and float numbers
>>> L4 = [ ‘ a ‘ ,  ‘ b ‘ ,  ‘ c ‘ ] # list of characters
>>> L6 = [  ] # list with no values i.e., empty list
>>> L7 = [ ‘ Orange ‘ ,  2 . 0 , 5 , [ 10 , 20 ] ] # nested list
>>> L8 = [ 7 ,  ” Hello ” ,  3 . 14 ] # list with mixed datatypes

Note that for string values you can use either single or double quotes with comma separator and numeric values are separated simply by comma separator. A list within another list is called nested list. Although a list can contain another list, the nested list still counts as a single element. So, the length of list L7 is four.

A list that contains no elements is called an empty list; you can create one with empty brackets [], as shown above. You can also create a long list containing many elements by splitting the list across several lines. Note that opening and closing square brackets appear just in the beginning and at the end of the list.

evenmun = [ 0 , 2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 , 18 , 20 , 22 , 24 , 26 , 28 , 30 , 32 , 34 , 36 , 38 ]
You can also create lists of single characters or digits using keyboard input. For example,

list1 = list ( input ( ‘ Enter elements of list: ‘ ) )
Enter elements of List: 2468
>>> list1
[‘2’ , ‘4’ , ‘6’ , ‘8’]

Notice you have entered integers, but the data type of all entered characters entered is a string. To enter a list of integers, you use the eval( ) method as given below:
list2= eval ( input ( ” Enter list: ” ) print ( list2 )

The string is a sequence of characters and a list is a sequence of values, but the list of characters is not the same as a string. To convert from the string to the list of characters, you can use list( ):

>>> s = ‘ hello ‘
>>> t = list ( s )
>>> print ( t )
[ ‘ h ‘ , ‘ e ‘ , ‘ 1 ‘ , ‘ 1 ‘  , ‘ o ‘ ]

To create an empty list (the list with no items, its length is 0), you can also use the built-in list type object:

>>> L = list( ) # empty list
>>> L
[ ]
>>> len(L)
0

Since the list is the name of a built-in function, you should avoid using it as a variable name. The list function breaks a string into individual letters. If you want to break a string into words, you can use the split method:

>>> s = ‘ work is worship ‘
>>> t = s.split ( )
>>> print ( t )
[ ‘ work ‘ , ‘ is ‘ ,  ‘ worship ‘ ]

Lists can be multidimensional, i.e., nested inside other lists. Lists containing other lists can be used as a way of storing a matrix or table of information. A 3X3 matrix could be created by assigning a list of lists to a variable.

>>> matrix = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ,  [ 7 , 8 , 9 ] ]

Example
Creating list of five integers.

#Creating list
list_of_elements = [ ] #empty list
print(“Enter list of five integers”)
for i in range(5):
print(“Enter integer”, i)
userinput=int(input( ))
list_of_elements.append(userinput)
print(“The list is: “,list_of_elements)RUN
>>>
Enter a list of five integers
Enter integer 0
4
Enter integer 1
7
Enter integer 2
9
Enter integer 3
3
Enter integer 4
2
The list is: [4, 7, 9, 3, 2]
>>>

Concept of Mutable Lists

Lists are mutable. This means that the value of an item in a list can be modified with an assignment statement. In other words, you can change the contents of a list without creating a new list. Strings, on the other hand, cannot be changed or modified.
>>> num = [ 17 , 12 , 18 ]
>>> num [ 1 ] = 5
>>> print ( num )
[ 17 , 5 , 18 ]
The second element of num, which used to be 12, is now changed to 5. You can consider a list as a relationship between indices and elements. This relationship is called a mapping; each index “maps to” one of the elements.

Let’s Try

Consider the following examples to update the val-ues inside the list:

>>> List = [ 1, 2, 3, 4, 5, 6 ]
>>> print (list)
[1, 2, 3, 4, 5, 6]
>>> List [2] = 10;
>>> print (List)
[1, 2, 3, 4, 5, 6]
>>> a = [1, 2, “anything” , 4.0]
>>> print (a)
[1, 2, ‘anything’ , 4.0]
>>> a[2] = ‘something’
>>> print (a)
[1,2, ‘something’ , 4.0]

Leave a Reply

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