List in Python

LIST

  • List is an ordered collection and are commonly used to collect  homogeneous objects, but can also contain heterogeneous data.

#program to define List

l1 = [10, 20, 30, 40, 50]
l2 = [
10, 10.25, 30.40,"Techno Xpresss"]
print("l1 : ", l1)
print("l2 : ", l2)
print("type(l1) :", type(l2[0]))
print("type(l2) :", type(l2[1]))

#output

l1 : [10, 20, 30, 40, 50]
l2 : [
10, 10.25, 30.4, 'Techno Xpresss']
type(l1) : <
class 'int'>
type(l2) : <
class 'float'>


  • To define a list, we use [ ] (square brackets), each member is separated by ','
                    Syntax :
                                List_name = [member1, member2,..]



#program to define List

l1 = [10, 20, 30, 40, 50]
print("l1 : ", l1)
print("type(l1) :", type(l1))

#output

l1 : [10, 20, 30, 40, 50]
type(l1) : <
class 'list'>


  • List can be defined as empty List.

#program to define empty List

l1 = []
print("l1 : ", l1)
print("type(l1) :", type(l1))

#output

l1 : []
type(l1) : <
class 'list'>


  • List can be defined with single member

#program

l1 = [10]
print("l1 : ", l1)
print("type(l1)", type(l1))


#output
l1 : [10]
type(l1) <class 'list'>



  • To access individual element of List, indexing is used.

#program to call a member of List by index

l1 = [10, 20, 30, 40, 50]
print("l1 : ", l1[0])
print("type(l1[0]) :", type(l1[0]))

#output

l1 : 10
type(l1[0]) : <class 'int'>


  • List allows Positive and negative indexing.

#program tocall member by positive and negative indexing in List

l1 = [10, 20, 30, 40, 50]

print("l1[0] : ", l1[0])
print("l1[-5] : ", l1[-5])

print("l1[5] : ", l1[4])
print("l1[-1] : ", l1[-1])

print("l1[2] : ", l1[2])
print("l1[-3] : ", l1[-3])


#output

l1[0] : 10
l1[-5] : 10
l1[5] : 50
l1[-1] : 50
l1[2] : 30
l1[-3] : 30


  • List allows multiplication and addition operations.

#program to understand addition and multiplication in List

l1 = [10, 20, 30, 40, 50]
l2 = [10, 10.25, 30.40,"Techno Xpresss"]
print("l1 : ", l1)
print("l2 : ", l2)
print("l1 + l2 : ", l1+l2) # add 2 list
print("l1 * 2 : ", l1*2) # multiple list1 with 2
print("l2 * 3 : ", l2*3) # multiple list2 with 3

#output

l1 : [10, 20, 30, 40, 50]
l2 : [10, 10.25, 30.4, 'Techno Xpresss']
l1 + l2 : [10, 20, 30, 40, 50, 10, 10.25, 30.4, 'Techno Xpresss']
l1 * 2 : [10, 20, 30, 40, 50, 10, 20, 30, 40, 50]
l2 * 3 : [10, 10.25, 30.4, 'Techno Xpresss', 10, 10.25, 30.4, 'Techno Xpresss', 10, 10.25, 30.4, 'Techno Xpresss']


adding 2 list, will cascade 2nd list in 1st list.
multiplying list with any number n, resulted list will have original list data n times.

    • Limitation:
      • we can not add any data to list via addition

#program to add any value to List

l1 = [10, 20, 30, 40, 50]
print("l1 : ", l1)

print("l1 + 60 : ", l1 + 60)

#output

l1 : [10, 20, 30, 40, 50]
Traceback (most recent call last):
File "
C:/Self_Practice_Er_M_S_Dandyan/Python/Pycharm/xml/main.py", line 68, in <module>
print("l1 + 60 : ", l1 + 60)
TypeError: can only concatenate list (not "int") to list


      • we can not multiply 2 list with each other

#program to try multiplying 2 List

l1 = [10, 20, 30, 40, 50]
l2 = [
10, 20, 30]
print("l1 : ", l1)
print("l2 : ", l2)
print("l1 * l2 : ", l1*l2)

#output

l1 : [10, 20, 30, 40, 50]
l2 : [
10, 20, 30]
Traceback (most recent call last):
File "
C:/Self_Practice_Er_M_S_Dandyan/Python/Pycharm/xml/main.py", line 69, in <module>
print("l1 * l2 : ", l1*l2)
TypeError: can't multiply sequence by non-int of type 'list'




    #program to use slice operator List

    l1 = [10, 20, 30, 40, 50, 60, 70, 80, 90]
    print("l1 : ", l1)
    print("l1[ : : ] : ", l1[ : : ])
    print("l1[2:10:3] : ", l1[ 2:10:3])
    print("l1[-9: : ] : ", l1[ -9: : ])
    print("l1[ : :-1] : ", l1[ : : -1])


    #output

    l1 : [10, 20, 30, 40, 50, 60, 70, 80, 90]
    l1[ : : ] : [
    10, 20, 30, 40, 50, 60, 70, 80, 90]
    l1[
    2:10:3] : [30, 60, 90]
    l1[-
    9: : ] : [10, 20, 30, 40, 50, 60, 70, 80, 90]
    l1[ : :-
    1] : [90, 80, 70, 60, 50, 40, 30, 20, 10]


    • Element values in List can repeat

    #program

    l1 = [10, 20, 30, 40, 50, 40, 30, 20, 10]
    print("l1 : ", l1)

    #output

    l1 : [10, 20, 30, 40, 50, 40, 30, 20, 10]


    • List are mutable, means we can modify members value

    #program to modify any particular member List

    l1 = [10, 20, 30, 40, 50, 40, 30, 20, 10]
    print("l1 : ", l1)
    l1[
    4] = 90
    l1[8] = 50
    print("l1 : ", l1)

    #output

    l1 : [10, 20, 30, 40, 50, 40, 30, 20, 10]
    l1 : [
    10, 20, 30, 40, 90, 40, 30, 20, 50]






    Comments

    My photo
    Techno Xpresss
    Bangalore, Karnataka, India