Topics

Deleting List Elements & other Functions

Welcome to another tutorial, here you get involved in Deleting an element from a List. In your program, there might be a need to delete some elements from a list. So, you will learn some of the methods you can employ to delete an element from a given list.

 

pop( ) function:

The pop() function typically requires only the index number of the element that needs to be deleted. Check out the example below:

>>> myList.pop(4)

Because the 4th index is the 5th element, as the index numbers start from 0. 

 

del keyword:

The del keyword needs the index number just like the pop() function.

>>> del myList[4]

From the example above, the 5th element will be deleted from the list. Also, we can combine this with slicing in other to delete a sequence of elements at the same time. This is shown below:

>>> del myList[3:7]

This will remove all elements from index 3 to 6.

 

remove( ) function: 

The remove() function only requires the value that has to be deleted, rather than the index number of the element to be deleted. 

For example,

>>> myList = ['Apple', 'Orange', 'Apple', 'Guava']
>>> myList.remove('Apple')

From the above, the function will remove the first apple element from the given list, but other elements with an apple as a value will not be deleted. The new list will look like as shown below:

>>> print (myList);

Output:

[ 'Orange', 'Apple', 'Guava']

 

Convert a List to String 

Now, let’s move to another aspect of coding, how to convert a list to String, and thereby print it as an output.

 

Without using any loop

In the case where you want to use the for or while loops to convert a list to a string. This is shown in the example below:

mylist = ['butter', 'jam', 'curd']
myStr = ', '.join(mylist)
print (myStr);

Output:

butter, jam, curd

 

Now, suppose there is a list of int values; then:

mylist = [1, 11, 111]
myStr = str(mylist).strip('[]')
print (myStr);

Output:

1, 11, 111

 

Using for loop 

Suppose there is a list of int values: 

mylist = [1, 11, 111]
myStr = ''.join(str(e) for e in mylist)
print (myStr)

Output:

111111

 

More functions for Lists 

1. insert(int, item) 

The insert function can be used to insert values, anywhere in the list. The function takes the index as the first argument where the items will be inserted and the second argument is the value that has to be inserted. This is shown below:

>>> myList = ['Python', 'C++', 'Java', 'Ruby', 'Perl']
>>> myList.insert(1, 'JavaScript')
>>> print (myList)

Output:

['Python', 'JavaScript', 'C++', 'Java', 'Ruby', 'Perl']

Notice how the value 'JavaScript' got inserted at index the 1.

 

2. reverse() 

The reverse() function is used to reverse the order of the elements in the list.

>>> myList = ['Python', 'C++', 'Java', 'Ruby', 'Perl']
>>> myList.reverse()
>>> print (myList)

Output:

['Perl', 'Ruby', 'Java', 'C++', 'Python']

 

3. sort() 

The sort() function is an important function in everyday life applications. It arranges all elements of a given list in ascending or descending order.

>>> myList = ['Python', 'C++', 'Java', 'Ruby', 'Perl']
>>> myList.sort()
>>> print (myList)

Output:

['C++', 'Java', 'Perl', 'Python', 'Ruby']

 

Suppose a list consists of numbers then the example above will make the sequence in ascending order. But, if the list consists of strings then the elements will be sorted in lexicographic ascending order.

Now, how can you sort in descending order?

>>> myList.sort().reverse()

from the example above, the elements were initially sorted in ascending order, then we used the reverse() function to reverse the list.