Python Programming – Data Structures – List Using Square Brackets

In this Page, We are Providing Python Programming – Data Structures – List Using Square Brackets. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.

Python Programming – Data Structures – List Using Square Brackets

List creation

The list can be created in many ways.

Using square brackets

As discussed before, the most common way of creating a list is by enclosing comma-separated values (items) between square brackets. Simply assigning a square bracket to a variable creates an empty list.

>>> a= [ ]
>>> a
[ ]
>>> type ( a ) 
< type ' list ’ >

Using other lists

A list can also be created by copying a list or slicing a list.

>>> a= [ ' spam ' , ' eggs ' , 100 , 1234 ]
>>> b=a [ : ]
>>> b
[ ' spam ' , ' eggs ' , 100 , 1234 ]
>>> c=a [ 1 : 3 ]
>>> c
[ ' eggs ' , 100 ]

List comprehension

List comprehension provides a concise way to create a list. A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression in the context of the for and if clauses that follow it.

Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable or to create a sub-sequence of those elements that satisfy a certain condition. For example, creating a list of squares using an elaborate approach is as follows:

>>> squares= [ ]
>>> for x in range ( 10 ) :
. . . squares. append ( x**2 )
. . .
>>> squares
[ 0 , 1 , 4 , 9 , 16 , 25 , 36 , 49 , 64 , 81 ]

The same can be obtained using list comprehension as:

squares= [ x**2 for x in range ( 10 ) ] 
>>> squares
[ 0 , 1 , 4 , 9 , 16 , 25 , 36 , 49 , 64 , 81 ]

Alternatively, it can also be obtained using map ( ) built-in function:

squares=map ( 1ambda x : x**2 , range ( 10 ) )
>>> squares
[ 0 , 1 , 4 , 9 , 16 , 25 , 36 , 49 , 64 , 81 ]

The following list comprehension combines the elements of two lists if they are not equal:

>>> [ ( x , y ) for x in [ 1 , 2 , 3 ] for y in [ 3 , 1 , 4 ] if x !=y ]
[ ( 1 , 3 ) , ( 1 , 4 ) , ( 2 , 3 ) , ( 2 , 1 ) , ( 2 , 4 ) , ( 3 , 1 ) , ( 3 , 4 ) ]

and it’s equivalent to:

>>> combs= [ ]
>>> for x in [ 1 , 2 , 3 ] :
. . . for y in [ 3 , 1 , 4 ] :
. . . if x !=y :
. . . combs. append ( ( x , y ) )
. . . 
>>> combs
[ ( 1 , 3 ) , ( 1 , 4 ) , ( 2 , 3 ) , ( 2 , 1 ) , ( 2 , 4 ) , ( 3 , 1 ) , ( 3 , 4 ) ]

Using built-in function

The list can also be created using a built-in function list ( ). The list ( [iterable] ) function returns a list whose items are the same and in the same order as iterable items. The iterable can be either a sequence, a container that supports iteration, or an iterator object. If iterable is already a list, a copy is made and returned. If no argument is given, a new empty list is returned.

>>> list ( ( ' hi ' ' hello ' , ' bye ' ) )
[ ' hi ' , ' hello ' , ' bye ' ]
>>> list ( ' hello ' )
[ ' h ' , ' e ' , ' 1 ' , ' 1 ' , ' o ' ]
>>> list ( ( 10 , 50 , ) )
[ 10 , 50 ]
>>> list ( )
[ ]

Accessing list elements

Like string indices, list indices start at 0. To access values in a list, use the square brackets with the index or indices to obtain a slice of the list.

>>> a= [ ' spam ' , ' eggs ' , 100 , 1234 ]
>>> a [ 0 ] 
' spam ' 
>>> a [ 0 ] [ 1 ]
' p '
>>> a [ 1 : 3 ] 
[ ' eggs ' , 100 ]