Characteristics of python list

Characteristics of python list

Characteristics of python list

The crucial characteristics of Python List are listed below-

  1. Lists can contain any arbitrary objects.
  2. Lists are ordered.
  3. Lists elements can be accessed by index.
  4. Lists are mutable.
  5. Lists can be nested to arbitrary depth.

1. Lists can contain any arbitrary objects

A list can contain the same types of values

list = [ 10, 20, 12, 9 ]
print( list )
Output
[ 10, 20, 12, 9 ]

list = [ ‘Ajay’ , ‘Raju’, ‘Vipin’]
print( list )
Output
[ ‘Ajay’ , ‘Raju’, ‘Vipin’]

Or it can contain mixed values

list = [10, ‘Ajay’, 20 ]
print( list )
Output
[10, ‘Ajay’, 20 ]

2. Lists are ordered

The order in which we specify the list elements. It maintains the order of the list lifetime.
Example 1:- 

    a = [5, 7, 1, 15]
    print(a)
    Output
    [5, 7, 1, 15]

    a =5, 7, 1, 15]    [5, 7, 1, 15]

Example 2:-

list = [10, 20, 30, 40, 50 ]
if we want to get element 30 then we will write-
print( list [2] )
output
30

 3. Lists elements can be accessed by index

The indexing of a list is the same as an array starting from 0, 1 , 2 ….
we can easily access any element of the list from its index.

 

Characteristics of python list

 

Example 1:- 

list = [10, 20, 30, 40, 50 ]
if we want to get element 30 then we will write-
print( list [2] )
output
30

Example 2:-

list = [10, 20, 30, 40, 50 ]
if we want to get element 20 then
print( list[-4 ])
output
20

4. List are mutable

Once a list has been created, elements can be added, deleted, and shifted. Python provides a wide range of ways to modify a list. 
Example :-

list = [10, 20, 30, 40, 50 ]
if we want to change the fourth element then
list[3] = 7
print(list)
Output
[10, 20, 30, 7, 50 ]

5. Lists can be nested to arbitrary depth

A list can contain sublists, which in turn can contain sublists themselves and so on to arbitrary depth.

a = [ 1, 2, [ 10, 20, 30, [100, 500 ], 40, 50 ], 3, 4, 5 ]
print( a )
Output
 [ 1, 2, [ 10, 20, 30, [100, 500 ], 40, 50 ], 3, 4, 5 ]

Share in a Single Click

Leave a Reply

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