List


List

 

While int, float, complex and bool are Data classes that can hold single values, we may at times require multiple values to be processed under single variable name. We have str class , which can do this , in which we can save from one character to any number of Characters. This kind of Data are called Sequence Data types. In other Language we have Array or Structure Data type for this kind of Data. Both are not available in default Python. If Array Data type is required then we have to use it after importing 'array' module. Though Structure is not available direct, we can use Dictionary Data class like that we will discuss them in detail in later chapters.

 

Python provides multiple Sequence Data Classes. They are,

 

-List

-Tuple

-Set

-Dictionary

 

Even Strings are considered as Sequence of Characters.

 

List

 

List is a Sequence Data type which can contain multiple Data enclosed within square brackets[]. The individual elements of the List are mutable that means it can be modified or changed any time. The Elements inside the List are separated by commas. The List can be a

 

     - Null List

     -Multiple elements of same Data type

     -Multiple elements of same Data type

     -Another Sequence Data object

     -Another List also, this is called as Nested List

 

Few examples,

#an empty list

 

a=[]

print(a)

#List of integers

b=[1,2,3,4,5]

print(b)

 

#List of Floating point numbers

 

c=[4.4,3.32,6.758]

print(c)

 

#List of ComplexNumbers

d=[3+1j,4+3j]

print(d)

 

#List of Characters

e=['a','e','i','o','u']

print(e)

 

#List of Strings

f=["Ramesh","Suresh","Vignesh"]

print(f)

 

#List containing multiple data types

g=["Magesh",53,45.325]

print(g)

 

#List containing another List object

h=[[1,2,3],[3.5,7.9]]

print(h)

 

         

Output

[]

[1, 2, 3, 4, 5]

[4.4, 3.32, 6.758]

[(3+1j), (4+3j)]

['a', 'e', 'i', 'o', 'u']

['Ramesh', 'Suresh', 'Vignesh']

['Magesh', 53, 45.325]

[[1, 2, 3], [3.5, 7.9]]

 

The individual elements of a List can be accessed using the Index. Index is the Position of the Element in the List. Index should always be an Integer, but it can be both Positive and Negative values.

 

Positive Index value starts at 0 and the last element index is n-1, where n is the number of Elements. If the Index are given as Negative value, then index value -1 denotes the last element. -2 is last but one and so on. If the List contains 5 elements then -5 is the first element. So the first element's negative index is -n.

 

Slice Operator can be used to access multiple values of a List at the same time. Slice operator is nothing but giving a range of values as shown in syntax.

 

<sv>:<ev>,

 

Where sv is start value and ev is End value. Both the values are optional but ':' symbol is must. When given <sv>:<ev>, The selected elements will be from the index denoted by the start value to that of elements denoted by End value -1.

 

Example

b[2:5] -  from the index position 2 to index position 4

c[2:] - from the index position 2 to the last

e[:3]- from the beginning to the index position 2

g[:]- from the beginning to the last

 

Operators on List

 

The '+' Operator on Lists is used for concatenating 2 Lists that is combining them. The '*' operator is used to repeat the Elements 'n' times.

 

b=[1,2,3,4,5]

c=[4.4,3.32,6.758]

print(b+c)

print(b*3)

 

Output

[1, 2, 3, 4, 5, 4.4, 3.32, 6.758]

[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]

 

Altering a List

 

The elements of a List can be changed or replaced any time , the syntax is

<lv>[index]=<value>

Where lv is the list variable.

 

Example

 

b=[1,2,3,4,5]

c=[4.4,3.32,6.758]

c[1]=100.27

print(c)

 

Output

[4.4, 100.27, 6.758]

 

It is also possible to change multiple values at the same time. This can be achieved using Slice operator.

 

Example

 

b=[1,2,3,4,5]

c=[4.4,3.32,6.758]

b[1:4]=[20,30,40]

print(b)

 

Output

[1, 20, 30, 40, 5]

 

List Methods

append()

 

This method is used to add Elements to the end of the List.

 

b=[1,2,3,4,5]

c=[4.4,3.32,6.758]

c.append(1000)

print(c)

Output

[4.4, 3.32, 6.758, 1000]

 

extend()

b=[1,2,3,4,5]

c=[4.4,3.32,6.758]

c.extend([100,200,300])

print(c)

Output

[4.4, 3.32, 6.758, 100, 200, 300]

 

Deleting Elements

To delete an elements from the List there are 3 Methods available. There are,

-del()

-remove()

-pop()

 

del()

By using del method we can either delete a single element or can delete an entire List object.

syntax

del <listname[index]>

 

Example

del b[2]

By executing this statement the 3rd element in the List 'b' will be removed, the index position of elements to the back of 3rd to last will get reduced by 1

 

del b

 

By executing this statement the entire list is removed. After this statement if the list b is accessed it will generate an error.

 

remove()

By using remove method we can delete a single element by referring to the element itself.

 

Syntax

<listname>.remove(<element>)

 

Example

e.remove('i')

 

By executing this statement the element 'I' in the List 'e' will be removed. The index position of elements to the back of 'I' to the Last will get reduced by 1.

 

pop()

By using pop method we can delete a single element by referring to the index.

 

Syntax

<listname>.pop(<index>)

 

Example

f.pop(1)

 

By executing this statement the element at the position 1 in the List 'f' will be removed. The index position of elements to the back of index 1 to the Last will get reduced by 1.

 

clear()

 

To delete all the elements in a single go , we can use this method.

Syntax

<listname>.clear

This statement will delete all the Elements in the specified list and return a Null List.

 

insert()

 

This method is used to Insert a value at the desired place.

syntax

<listname>.insert(<insert position>,<value>)

 

Example

li=[5,50]

print(li)

li.insert(1,10)

print(li)

 

Output

[5, 50]

[5, 10, 50]

 

Insert Multiple values

 

To insert multiple values into the List, we can use as shown in the example below

 

li=[5,50]

print(li)

li.insert(1,10)

print(li)

li[2:2]=[15,20,25,30,35,40,45]

print(li)

 

Output

[5, 50]

[5, 10, 50]

[5, 10, 15, 20, 25, 30, 35, 40, 45, 50]

 

Other Methods

 

max()

Returns the maximum(highest) value in the List

min()

Returns the maximum(lowest) value in the List

sum()

Sum of the values in the List, can be used on Numerical List

sort()

Arranges the elements in the List in ascending or descending order

len()

Returns the count of number or elements in the List

count()

Counts the number of occurrences of a given value

index()

Returns the Position of the Element within the List

reverse()

Reverses the Elements in the List

cmp()

Compare function not available in Python 3 and above. instead use Relational Operators like <,<=,>,>=,==,!= for the same.

Examples

 

Reverse

b=[1,2,3,4,5]

print(b)

b.reverse()

print(b)

Output

[1, 2, 3, 4, 5]

[5, 4, 3, 2, 1]

 

Sort

a=[5,10,2,8,1]

print(a)

a.sort()

print(a)

Output

[5, 10, 2, 8, 1]

[1, 2, 5, 8, 10]

 

Len

 

a=[5,10,2,8,1]

print(len(a))

 

Output

5

 

Sum

a=[5,10,2,8,1]

print(sum(a))

 

Output

26

 

Count

a=[5,10,2,8,1]

print(a.count(5))

 

Output

1

 

Packing and Unpacking of List

 

The individual elements of a List can be assigned to variables directly without using Index variable , this is called Unpacking a List. Similarly a List can be generated from multiple variables also, this is called Packing of List.

 

Example for Unpacking

 

s=[103,"GST Road","Chennai"]

print(s)

print(type(s))

x,y,z=s

print(x)

print(type(x))

#Integer class

print(y)

print(type(y))

#string class

print(z)

print(type(z))

#string class

 

Output

[103, 'GST Road', 'Chennai']

<type 'list'>

103

<type 'int'>

GST Road

<type 'str'>

Chennai

<type 'str'>

 

Example for Packing

 

x="Welcome"

y="to Python"

z="Programming"

s1=[x,y,z]

print(s1)

print(type(s1))

 

Output

['Welcome', 'to Python', 'Programming']

<type 'list'>


No comments:

Post a Comment