Operations on lists in Python

Following are the list of operations in Python:

  • Accessing a list
  • Adding elements to a list
  • Deleting elements from list
  • Slicing a list
  • Updating a list
  • Mathematical operations on a list

Accessing a list:

Let us consider a below list and we will explain the different ways of accessing the list. 

sites = ["embeddeddesignblog","py-programmers","youtube-Way2know","TalentEve",1 , 2, 3]

Accessing a list using index: 

Elements in the above list can be accessing index. Index can be positive as well as negative. The index of starting element is 0 can increments for every element, 1 for accessing second element and so on. The last element of the list can be accessed using -1 and -2 for next element and so on.

sites[0]     // points to "embeddeddesignblog"
sites[1]    // points to "py-programmers"
sites[-1]   // points to 3
sites[-4]   // points to "TalentEve"

-------------------------------------------

Updating a list/Adding elements to a list:

Let us consider a below list and we will explain the different ways of updating the list. 

sites = ["embeddeddesignblog","py-programmers","youtube-Way2know","TalentEve",1 , 2, 3]

updating a list using index: 

Elements in the above list can be changed using it's index. 

# the below command changes the last element to 4
sites[-1]   = 4  

#after above operation, the list is now,
sites = ["embeddeddesignblog","py-programmers","youtube-Way2know","TalentEve",1 , 2, 4]

If a programmer tries to assign an element outside the index of list, then the interpreter throws an error as below:

sites[7] = 5

The above statement throws an error as the index of sites is only till 6 and throws below error

IndexError: list assignment index out of range


updating a list using append() method: 

sites = ["embeddeddesignblog","py-programmers","youtube-Way2know","TalentEve",1 , 2, 3]

Elements can be appended to the above list using append() method. Using append() a single element can be added to the list

sites.append("Myvision-thisworld")

The list shall be now,

['embeddeddesignblog', 'py-programmers', 'youtube-Way2know', 'TalentEve', 1, 2, 4, 'Myvision-thisworld']

We can see that "Myvision-thisworld" got added at the end of the list.

updating a list using insert() method: 

sites = ["embeddeddesignblog","py-programmers","youtube-Way2know","TalentEve",1 , 2, 3]

While the append() method can only add at the end, sometimes the requirement could be that elements need to be added at a specific index. insert() method is used for this purpose.

sites.insert(4, "Myvision-thisworld")

In the above insert method, 4 indicates the index and "Myvision-thisworld" indicates the actual string to be inserted

The list shall be now,

['embeddeddesignblog', 'py-programmers', 'youtube-Way2know', 'TalentEve', 'Myvision-thisworld', 1, 2, 4]

We can see that "Myvision-thisworld", got inserted at the index 4. Also, the other elements of the list are not disturbed

updating a list using extend() method: 

sites = ["embeddeddesignblog","py-programmers","youtube-Way2know","TalentEve",1 , 2, 3]

More than one element can be appended to the above list using extend() method. 

sites.extend(["Myvision-thisworld", 8, 9])

The list shall be now,

['embeddeddesignblog', 'py-programmers', 'youtube-Way2know', 'TalentEve', 1, 2, 4, 'Myvision-thisworld', 8, 9]

We can see that "Myvision-thisworld", 8, 9 got added at the end of the list.

removing elements from a list using remove() method: 

sites = ["embeddeddesignblog","py-programmers","youtube-Way2know","TalentEve",1 , 2, 3]

Any element of a list can be removed, using remove() method. The element to be removed has to be mentioned as parameter for this method

sites.remove(1)

The list shall be now,

['embeddeddesignblog', 'py-programmers', 'youtube-Way2know', 'TalentEve', 2, 4]

We can see that 1 is removed from the list.

Let us consider below list where there are two occurrences of element 1.

sites = ["embeddeddesignblog", 1, "py-programmers","youtube-Way2know","TalentEve",1 , 2, 3]

If the below remove method is used,

sites.remove(1)

Then the first occurrence of 1 shall be deleted which meant the list shall be now,

sites = ["embeddeddesignblog", "py-programmers","youtube-Way2know","TalentEve",1 , 2, 3]

removing elements from a list using pop() method: 

sites = ["embeddeddesignblog","py-programmers","youtube-Way2know","TalentEve",1 , 2, 3]

Any element of a list can be removed, using pop() method. The index of the element to be removed has to be mentioned as parameter for this method

sites.pop(0)

The list shall be now,

['py-programmers', 'youtube-Way2know', 'TalentEve', 1, 2, 3]

We can see that "embeddeddesignblog" at index 0 is removed from the list.

clearing a list using clear() method: 

sites = ["embeddeddesignblog","py-programmers","youtube-Way2know","TalentEve",1 , 2, 3]

All the elements of the list can be cleared using clear() method

sites.clear()
print(sites)

The list shall be now,

[]

We can see that all the elements in the list are cleared

-------------------------------------------

Mathematical operations on a list:

Let us consider a below list and see the mathematical related operations on this list. 

sites = ["embeddeddesignblog","py-programmers","youtube-Way2know","TalentEve",1 , 2, 3]

Finding the number of occurrences of an element in the list using count() method: 

print(sites.count("py-programmers"))

The above method prints 1 as "py-programmers" is appearing only once in the list

Finding the length of the list using len() method: 

print(len(sites))

The above method prints 7 as the number of elements in the list are 7.

Concatenating two lists: 

Let us have two lists as below:

list_1 = [1,2,3]
list_2 = [3,4,5]

print(list_1+list_2)

The above operation prints [1,2,3,3,4,5] after concatenating two lists.

Finding the minimum and maximum of the list using min(), max() method: 

print(min(sites))
print(max(sites))

Trying to find the minimum and maximum of the list with different data types is not supported. All the above elements must be of single datatype. The above operation throws below error:

TypeError: '<' not supported between instances of 'int' and 'str'

When the elements are of the same data type as below, minimum and maximum operation can be performed

sites = ["embeddeddesignblog","py-programmers","youtube-Way2know","TalentEve"]
print(max(sites))
print(min(sites))

The output of the above maximum and minimum shall be 

youtube-Way2know
TalentEve

The minimum and maximum values are identified here based on the ASCII values of the characters from the starting of the string.

Multiplying a list: 

A given list can be replicated n number of time using multiplication

list_1 = [1,2,3]
print(list_1*2)

The above operation prints [1,2,3,1,2,3] as the list is replicated using multiplication.

-------------------------------------------

Slicing a list:

Let us consider a below list and we will explain the different ways of accessing the list. 

sites = ["embeddeddesignblog","py-programmers","youtube-Way2know","TalentEve",1 , 2, 3]

Slice operation is used to access specific sections of a list. If for example, a programmer wants to print first two elements of a list, then slice operation can be used. Even though index can be used to access, slicing operation is a single statement where multiple list elements can be selected.

print(sites[0:2])

The above prints first two elements after the slicing operation. The output shall be 

['embeddeddesignblog', 'py-programmers']

Note that starting from 0 till 1 shall be sliced, element at 2 is excluded

#the below slicing prints all elements of a list
print(sites[:])

#the below slicing prints starting from 0 till 2
print(sites[:2])

#the below slicing prints starting from 0 till the end
print(sites[0:])

#the below slicing prints the last element of the list
print(sites[-1:])

#the below slicing prints the from index -4 till the end -1
print(sites[-4:])
-------------------------------------------

Post a Comment

0 Comments