Unleashing the Dynamic Power of Python Collections List 2024

python collections

Python programming language supports five python collections data types.

1. List
List is a python collections which is ordered and mutable (changeable) and it allows duplicate members.

2. Array
Array is a data structure that stores values of same data type.

3. Tuple
Tuple is a python collections which is ordered and immutable and it allows duplicate members.

4. Set
Set is a collection which is unordered and unindexed and it does not allow duplicate members.

5. Dictionary
Dictionary is a collection which is unordered, mutable and indexed and it does not allow duplicate members.

Efficiency of the program depends on the selection of appropriate collection type.

Programmer has to understand the requirement and purpose of logic and then select the appropriate data structure suitable to it.

Python Collections – Lists

1) List is a python collections of members which are ordered and changeable.
2) In Python, list are written with square brackets.
3) List members are separated by comma.
4) List members may contain any type of data.
5) Python allows nested lists i.e. a list member can be another list.
6) List members are called elements or items.
7) List elements are accessed using index.
8) Index is always an integer and starts with zero.
9) Nested lists are accessed using nested indexing.
10) Python also allows to access members in reverse using negative indexing.
11) Index of last element is -11, that of second last is -2, that of third last is -3 and so on.

Creating Empty List (Python Collections)

  • List can be created using empty square brackets or using list constructor.

    Examples to declare empty list:
Example 1:

a = list()            #using constructor
print(a)

Output: []


Example 2:

b=[]                 #by assigning empty[]
print(b)

Output: []

Writing Lists (Python Collections)

  • List name in print() prints all members.
  • For printing particular member, we need to specify its index number.
Example 1: 

num = [1,7,3,9,22,45,67,90,12]
print(num)

Output: 
[1, 7, 3, 9, 22, 45, 67, 90, 12]

Example 2:

city = ['DEL','BOM','BLR','HYD']
print(city)

Output: 
['DEL', 'BOM', 'BLR', 'HYD']

Example 3:

city = ['DEL','BOM','BLR','HYD']
print(city[0])

Output:
DEL

Example 4:

city = ['DEL','BOM','BLR','HYD']
print(city[-1])

Output:
HYD

Example 5:

city = ['DEL','BOM','BLR','HYD']
for x in city print(x)

Output:
HYD

Example 6:

city = ['DEL','BOM','BLR','HYD']
for x in city: print(x)

Output:
DEL
BOM
BLR
HYD

Example 7:

mix=['zero',1,'two',3.0,4,'five',6.0]
print(mix)

Output:
['zero', 1, 'two', 3.0, 4, 'five', 6.0]

Check whether list is empty (Python Collections- List)

  • Following are 3 different methods to test any list for emptiness.
#method 1

l1 = []

if not l1:
    print("The list is empty")
else:
    print("List is not empty")
    
Output:

The list is empty


#method 2

l1 = []

if not len(l1):
    print("The list is empty")
else:
    print("List is not empty")

Output:

The list is empty


#method 3

l1 = []

if not l1:
    print("The list is empty")
else:
    print("List is not empty")

Output:

The list is empty

List as matrix i.e.array

  • Although python provides separate array module, we can also represent list as array.
  • Array is finite set of homogeneous elements and while represent list as array, necessary condition is that all elements must be of same type.
  • Array may be one dimensional or multi dimensional.
  • Horizontal entries are rows and vertical entries are columns.
  • In an array of size mxn, there are m rows and n columns.
  • Each row contains n entries whereas each column contain m entries.
  • Two dimensional array (matrix in math) is represented using nested list i.e. lists within list, where each element is treated as row of matrix.
Example 1:
x= [[1,2,3,4],[5,6,7,8]]   #size is 2 * 4
print(x)

Output:
[[1, 2, 3, 4], [5, 6, 7, 8]]

Example 2:
x = [[1, 2, 3, 4], [5, 6, 7, 8]]
result = len(x)
print(result)

Output:
2

Example 3:
x = [[1, 2, 3, 4], [5, 6, 7, 8]]
result = len(x[0])
print(result)

output:
4


Example 4:
x = [[1, 2, 3, 4], [5, 6, 7, 8]]
result = len(x[1])
print(result)

output:
4
  • Here, x is a two dimensional matrix and
    Row 1 is 1 2 3 4
    Row 2 is 5 6 7 8
    Row numbers are 0,1
    Column numbers are 0,1,2,3
  • Element at first row and first column is x[0][0]=1 and last element is a[1][3]=8
Code to print elements of 2 D array is as:

a = [[1, 2, 3, 4], [5, 6, 7, 8]]

for r in a:
    for j in r:
        print(j, end=' ')
    print()
    
Output:

1 2 3 4 
5 6 7 8 

Leave a Reply

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