Working with Python - List - Function and Method

Introduction

In this blog post we are trying to continue our journey with Python. It contains List concept, List function and List method.

Hope it will be interesting.


Updating the List 

Suppose we have a list and we need to update one of the values of the List item.

Please remember that the indexes of the list start from 0.

Here is the example:

 

list=['Joydeep','Mayuree','Saurrya','Sai','Arnab','Pavan','Srilekha']

print(list)

# Now I need to replace ‘Joydeep’ with ‘Shradha’. Here the position of Joydeep index is 0

list[0]='Shradha'

print(list)

 

['Joydeep', 'Mayuree', 'Saurrya', 'Sai', 'Arnab', 'Pavan', 'Srilekha']

['Shradha', 'Mayuree', 'Saurrya', 'Sai', 'Arnab', 'Pavan', 'Srilekha']

 

 

Note : Instead of single course we can use double course also

 

list=["Joydeep","Mayuree","Saurrya","Sa","Arnab","Pavan","Srilekha"]

print(list)

# Now I need to replace ‘Joydeep’ with ‘Shradha’. Here the position of Joydeep index is 0

list[0]="Shradha"

print(list)

 

['Joydeep', 'Mayuree', 'Saurrya', 'Sai', 'Arnab', 'Pavan', 'Srilekha']

['Shradha', 'Mayuree', 'Saurrya', 'Sai', 'Arnab', 'Pavan', 'Srilekha']

 

 

Deleting Value from the List

 

To delete a specified element from the List we are using del keyword.

 

Example:

 

list=["Joydeep","Mayuree","Saurrya","Sa","Arnab","Pavan","Srilekha"]

print(list)

del list[1]

print(list)

 

['Joydeep', 'Mayuree', 'Saurrya', 'Sa', 'Arnab', 'Pavan', 'Srilekha']

['Joydeep', 'Saurrya', 'Sa', 'Arnab', 'Pavan', 'Srilekha']

 

 

Addition of List

 

It just concatenates two lists.

Example:

 

list_1=["Joydeep","Mayuree","Saurrya"]

list_2=["Sa","Arnab","Pavan","Srilekha"]

list_3=list_1 + list_2

print(list_3)

 

['Joydeep', 'Mayuree', 'Saurrya', 'Sa', 'Arnab', 'Pavan', 'Srilekha']

 

 

Multiplication of List

 

list_1=["Joydeep","Mayuree","Saurrya"]

list_2=list_1 * 2

print(list_2)

 

['Joydeep', 'Mayuree', 'Saurrya', 'Joydeep', 'Mayuree', 'Saurrya']

 

List Function

 

Len()

 

list_1=["Joydeep","Mayuree","Saurrya"]

length=len(list_1)

print(length)

 

à 3

 

Max()

 

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

print(max(list))

 

à5

 

List()

This is Conversion function, Used to convert into list.

 

Example:

touple=(1,2,3,4)

print(touple)

list=list(touple)

print(list)

 

(1, 2, 3, 4)

[1, 2, 3, 4]

 

Sorted()

 

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

print(sorted(lst))

 

[1, 2, 3, 4, 5]

 

Note: if we used sorted in tuple it always returns list. If we use sorted in a list that have string value, it just take the first character of string and find there ASCII value and them sort accordingly.

 

Now we have to sort it descending order.

Syntax is:  sorted(list, reverse = True)

 

Again Note: in python True/False is Boolean data type Not true/false. First character must be in capital.

 

lst=[5,2,3,1,0]

print(sorted(lst,reverse=True))

 

[5, 3, 2, 1, 0]

 

 

List Method

 

Append()

Used to append a value in list

 

lst=["Joydeep","Mayuree"]

lst.append("Saurrya")

print(lst)

 

['Joydeep', 'Mayuree', 'Saurrya']

 

 

Extend()

lst_1=["Joydeep","Mayuree"]

lst_2=["Sai","Arnab"]

lst_1.extend(lst_2)

print(lst_1)

 

['Joydeep', 'Mayuree', 'Sai', 'Arnab']

 

Difference between append and extend

 

Linux = ["kali", "Ubuntu", "debian"]

Linux2 = ["RHEL", "Centos"]

Linux.extend(Linux2)

print(Linux)

 

['kali', 'Ubuntu', 'debian', 'RHEL', 'Centos']

 

Linux = ["kali", "Ubuntu", "debian"]

Linux2 = ["RHEL", "Centos"]

Linux.append(Linux2)

print(Linux)

 

 ['kali', 'Ubuntu', 'debian', ['RHEL', 'Centos']]

 

Please look at the output, hope you can understand that.

 

Count()

lst=["joy","toy","boy",1,"joy",3,"joy"]

print(lst.count("joy"))

 

à3

 

Index()

 OS = ['kali', 'Ubuntu', 'debian', 'RHEL', 'Centos']

 OS.index("debian")

à2

 

Insert()

 

Syntx: List.insert(Index, Item)

 

A = [‘iron-man', 'hulk', 'Thor']

 A.insert (0,"Captain-America")

Print(A)

 

['Captain-America', 'iron-man', 'hulk', 'Thor']

 

Remove()

 

Avengers1 = ["Iron-man","Thor","Loki","hulk"]

Avengers1.remove ("Loki")

print(Avengers1)

 

['Iron-man', 'Thor', 'hulk']

 

Pop()

Pop remove the last element of List.

 

Avengers1 = ["Iron-man","Thor","Loki","hulk"]

Avengers1.pop ()

print(Avengers1)

 

['Iron-man', 'Thor', 'Loki']

 

 

 

Hope you like it.


Comments

  1. These are very simple yet fundamentally very important concepts.I personally like the difference between extend and append

    ReplyDelete
  2. Can we use index of a position of list in remove?

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete

Post a Comment

Popular Posts

Working with Python – Map, Filter and Reduce Function

Working with Python – Module

Copying Multiple File from Blob Storage to Single SQL Table – Part-2