List Methods in Python

237

As we have learned in previous videos, if we want to store some data, python provides variables to do this. But, what if we want to store a set of data?

For this purpose, we will have to create more variables. But, creating so many variables is a tedious task to do. Also, we will have to choose so many names for these variables which will result in confusion for the programmer. To avoid this, we have the concept of a list in python. This is similar to the concepts of the array in C and C++.

In lists, the data are stored at memory locations, which can be accessed by the address, which resolves the problem of thinking and choosing too many names for different variables.

Now, we will learn about the lists in Python –

List In Python

  • A list is a collection of different values or different types of items.
  • Unlike an array in C/C++, it is capable of storing more than one type of value.
  • Items in the list are enclosed within the square brackets ( [] ) , and separated with the comma ( , ).

How to define a list in python?

 a = [“harry” , 1 , “johnny”, 9.5]

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

When we create a list, the elements are indexed with a specific index which starts from 0 and continues till n-1. Where n denotes the total number of elements in the list. This type of indexing is called forward indexing.

Apart from this, there is one more way which is called backward indexing. In this, the last element is indexed as -1 and the second last as -2, and this way it continues backward till –n, where n denotes the number of elements.

Through the concept of indexing, we can jump to a specific element and update or modify the content at that index.

We will understand it better with the help of the example given below –

A = [“john” , 5 , “shyam” , 11.5]

These are indexed as such in the list –

If we want to modify the content, for example, we want the replace the value 1 stored in the index ‘1’ to 5, so we can do this with the help of its index.

If we write A[1] = 5

The current value at the index ‘1’ will be updated to ‘5’.

Now, we will learn about some useful keywords that are used in lists.

List Slicing in Python

We can use the slicing operator to access a range of list –

my_list = ['y','u','v','a','y','a','n','a']

# elements from index 2 to index 4

print(my_list[2:5])

# elements from index 5 to end

print(my_list[5:])

# elements beginning to end

print(my_list[:])

Output –

[‘v’, ‘a’, ‘y’]

[‘a’, ‘n’, ‘a’]

[‘y’, ‘u’, ‘v’, ‘a’, ‘y’, ‘a’, ‘n’, ‘a’]

Add/Change List Elements

We can change the values at any position in a List. That’s why, lists are said to be mutable.

For example –

odd = [2,4,6,8]

odd[0] = 1

print(odd)

Output –

[1,4,6,8]

Delete List Elements

Python Del statement can be used to delete one or more items from a list. It can even delete the list directly.

Example –

my_list = [‘h’, ‘a’, ‘r’, ‘s’, ‘h’, ‘i’, ‘t’]

del my_list[2]

print(my_list)

 Output –

[‘h’, ‘a’, ‘s’, ‘h’, ‘i’, ‘t’]

Similarly, there are many more functions of the Python list which are mentioned below with their uses.

Now, we will be discussing all of these list functions in detail –

Append() –

This method adds an element to the end of the existing array.

Syntax –

array_name.append(new_element)

Example –

list1 = ['good', 'boy']

list1.append('studies')

print (list1)

Output –

good boy studies

Extend() –

This method is used to add all the elements of one list to at the end of another list.

Syntax –

First_list.extend(second_list)

This will insert the second list at the end of the first list.

Example –

list_1 = [1,4,5,6,2]

list_2 = [11,12,15]

list_1.extend(list_2)

print('List after extend():', list_1)

Output –

1,4,5,6,2,11,12,15

Insert() –

This method is used to insert a value at a specific position in a list.

Syntax –

list_name.insert(pos,element)

Example –

students = ['ram', 'shyam', 'rohit']

students.insert(1, "rahul")

Output –

[‘ram’, ‘rahul’, ‘shyam’, ‘rohit’]

Remove() –

This function removes a specific value from a list. In case a value occurs multiple times, it removes the first occurrence of that value.

Syntax –

list.remove(element)

Example –

list1= [2, 3, 5, 7, 9, 11]

list1.remove(9)

print('Updated List: ',list1)

Output –

Updated List:  [2, 3, 5, 7, 11]

Pop() –

This function deletes and returns the last value of the value at specified index from the list

Syntax –

list_name.pop(index)

Here index is passed only if we want to delete a specific value

Example –

list1 = [1, 2, 3, 4, 5, 6]

print(list1.pop(), list1)

print(list1.pop(0), list1)

Output –

[1, 2, 3, 4, 5]

[2, 3, 4, 5]

Clear() –

This function removes all the elements from the list.

Syntax –

list.clear()

Example –

students = ['ram', 'shyam', 'rohit']

students.clear()

Output –

[]

Index() –

This method searches the whole list and as soon as the value is found, it returns its index.

Syntax –

list_name.index(element, start, end)

In this, start, end is optional if we want to search between specific positions

Example –

list2 = ['harshit', 'is', 'a', 'good', 'boy']

print(list2.index('is'))

Output –

1

Count() –

This function returns the number of elements with a specific value

Syntax –

list.count(value)

Example –

students = ['ram', 'shyam', 'rohit', 'ram']

x = students.count("ram")

print(x)

Output –

2

Sort() –

This function sorts a list in ascending order

Syntax –

list.sort()

Example –

numbers = [11, 3, 7, 5, 2]

numbers.sort()

print(numbers)

Output –

[2, 3, 5, 7, 11]

Reverse() –

This method reverses the entire list, i.e., The first element becomes the last and the last becomes first.

Syntax –

list_name.reverse()

Example –

list1 = [1, 2, 3, 4, 1, 2, 6]

list1.reverse()

print(list1)

Output –

[6, 2, 1, 4, 3, 2, 1]

Copy() –

This function returns a shallow copy of the list.

Syntax –

new_list = list.copy()

Example –

list = [2, 3, 5]

numbers = list.copy()

print('Copied List:', numbers)

In this article, we have learned about the list and some of the useful functions that are used to manipulate lists. These are used very Often by the programmers of use the list by modifying them

Related Quiz

Previous QuizChecked and Unchecked Exceptions in Java
Next QuizTuples in Python

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.