Lists

 

Exploring List

  • List is similar to an array but lists can store different types of elements.

  • Lists are represented by a square bracket

student = [ 101 , ‘abc’ , 50 ]

  • First element is student [ 0 ] which is 101 and so on.

  • print ( student [ 0:3 ] ) will display [101, 'abc', 50]

  • print ( student [ 0 : 3 : 2 ] ) will display [ 101, 50 ] with step of 2 that is student[0].. Student[2] …

Creating lists using range() function

  • Range function is used as range( start, stop, stepsize ) By default stepsize is 1

for n in range ( 0, 10, 2 ) :

print ( n ) // it will print 0,2,4,6,8

  • Creating list

lst = list( range( 0,10,2 ) )

print( lst )

Creating lists using range() function

  • Printing the list using while loop

n=0

while n < len( lst ):

print( lst [n] )

n=n+1

  • Printing the list using for loop

for n in lst :

print ( n )

Updating the elements of the list

  • The lists are mutable means we can append, update or delete the contents of the list.

lst = list( range( 1, 5 ) )

  • Append() function appends a new element at the end of list

lst.append( 8 )

  • Update the contents of the list

lst [0] = 9

lst [ 1: 3 ] = 10, 11

[0] [1] [2] [3] [4]

Concatenation of two lists

+’operator is used to concate two list

Lst1=[1,2,3]

Lst2=[‘a’,’b’]

Lst3=lst1+lst2

Print(lst3)

Repetition of lists

We can repeat the elements of a list ‘n’ number of times using ‘*’ operator.

Lst=[1,2,3]

Print(lst*2)


Membership in lists

We can check is an element is a member of list or not with use of membership operator.

If the element is in list then it returns true when we use ‘in’ operator else it returns false.

If the element is not in the list then the ‘not in’ operator returns true else returns false.

Aliasing and Cloning lists

  • Provide another name to existing list is called aliasing a list.

  • If l1 is existing list then,

  • l2=l1 will create an alias of the l1. here l1 and l2 both refer to same memory location

  • Obtaining exact copy of the existing list is called cloning list.

  • If l1 is existing list then,

  • L2=l1[:] will create clone of l1. here l1 and l2 both are different and store on different memory location.

Example:

#list

student=[101,'abc',50]

print(student)

print(student[0:3])

print(student[0:3:1])

print(student[0:3:2])

#create list from range

for n in range(0,10,2):

print(n)


lst=list(range(0,10,2))

print(lst)


#printing list using while loop

n=0

while n<len(lst):

print(lst[n])

n=n+1


#printing list using for loop

print("List with for loop")

for n in lst:

print(n)


#updating the elements of list


lst=list(range(1,5))

print(lst)

lst.append(8)

print(lst)

del lst[0]

print(lst)

lst.remove(8)

print(lst)

lst.reverse()

print(lst)

#concatenatoion of two list

l1=[1,2,3]

l2=['a','b']

l3=l1+l2

print(l3)

#Repetition of list

l4=l3*2

print(l4)

#membership in list

m_lst=[10,20,30,40]


x=int(input("enter element"))


if x in m_lst:

print(x, "is present in list")

else:

print(x, "is not present in list")

#alias and cloning the list

print("Alias")

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

l2=l1

print("l1 is", l1)

print("l2 is", l2)

l2[0]=20

print("l1 is", l1)

print("l2 is", l2)

print("Cloning")

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

l2=l1[:]

print("l1 is", l1)

print("l2 is", l2)

l2[0]=20

print("l1 is", l1)

print("l2 is", l2)

Methods to process List


Method

Example

Desciption

Sum()

List.sum()

Return sum of all elements

Index()

List.index(a)

Return first occurrence of a in list

Append()

List.append(a)

Append a at the end of list

Insert()

List.insetr(I,a)

Insert a in the list at I index

Sort()

List.sort()

Sort in ascending order

Pop()

List.pop()

Removing the ending element of list

Remove()

List.remove(a)

Remove a from the list

Count()

List.count(a)

Return number of occurrence of a in list

Extend()

List.extends(list1)

Append list1 to list

Copy()

List.copy()

Copies all the list elements into a new lsit and returns it

Reverse()

List.reverse()

Reverse the elements of list

clear()

List.clear()

Clear the list elements


Example:

l1=[1,2,3,4,5,6,7]

print(l1);

print("Sum method", sum(l1))

print("length method", len(l1))

print("Index of 5 in list",l1.index(5))


l2=[6,7]

l1.append(l2)


print("append method", l1)

l1.append(8)

print("append method", l1)


l1.insert(0,99)

print("after insert method", l1)

l3=l1.copy()

print("copy method", l3)

l4=[10,20]

l4.extend(l1)

print("extend method", l4)

print("count method 1 in list1", l1.count(1))

l4.remove([6,7])

print("remove method", l4)


l5=[1,2,3,4,5,6,7]

print("pop method", l5.pop())


l5.reverse()

print("reverse method", l5)


l5.sort()

print("sort method")

print(l5)


l5.clear()

print("Clear method")

print(l5)

Nested Lists

We can write list within another list and it is called nested list.

L1=[1,2,3]

L2=[4,5,6,l1]

Example:

l1=[1,2,3]

l2=[4,5,6,l1]

print("list 1",l1)

print("list 2",l2)

print("Complete list")

for i in l2:

print(i)

print("Elements from nested list")

for i in l2[3]:

print(i)

Output:

('list 1', [1, 2, 3])

('list 2', [4, 5, 6, [1, 2, 3]])

Complete list

4

5

6

[1, 2, 3]

Elements from nested list

1

2

3