C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Python

List Operations in Python - II

Learn: In this article, we will going to discuss about various list operations as updating, deleting, and other functions and methods to accommodate list elements (with examples).
Submitted by Abhishek Jain, on October 12, 2017

In the previous article, we have discussed about the accessing, traversing and appending operations associated with the lists. To better understand this please go through the previous one Lists Operations in Python-I.

4) Updating List Elements

Updating of an element of the list is, accomplished by accessing the element & modifying its value in place. In python, it is possible to modify a single element (by using index to access single element) or a part of list (using list slice).

Example: Let's update a slice

>>> L= [1, 2, 3, 4, 5, 6]
>>> L [1:2] = [10, 20]
>>> print L

Output

[1, 10, 20, 4, 5, 6]

As lists are sequences, they support many operations of strings. Operator +&* results in concatenation & repetition of lists. '+' operator in lists expects the same type of sequence on both sides otherwise throws a type error.

Example

1)

>>> L= [10, 20, 30, 40, 50]
>>>L1=["C","C++","Java","Python"]
>>> L2=L+L1
>>> print L2

Output

[10, 20, 30, 40, 50, 'C', 'C++', 'Java', 'Python']

2)

>>>["Hi!"]*3

Output

['Hi!', 'Hi!', 'Hi!']

5) Deleting List Elements

There are many ways to delete/remove element(s) from the list:

  1. We can use pop() or del, if index is known.
  2. If the element is known, remove() can be used.
  3. To remove more than one element, del() with list slice can be used.

i) pop()

It pops out/removes the element from the given index, and return the element which wasremoved.

Syntax:

List.pop ([index])

Example

>>>	L1= ["C","C++","Java","Python"]
>>>	L1.pop(2)
>>>	print L1

Output

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

del removes the specified value from the list, but does not return the deleted element.

Example

>>>	L1= ["C","C++","Java","Python"]
>>>	del L1[3]
>>>	print L1

ii) remove()

If we know the element to be deleted but not the index of the element, then remove () can be used.

Example

>>>	L= [23, 34, 24, 56, 89]
>>>	L.remove (34) #will remove the value 90 from the list
>>>	print L1

Output

[23, 24, 56, 89]

iii) del() with slicing

Example

>>>	L= [1,2,3,4,5,6]
>>>	del L1[2:4] #will delete 2 and 3 index element from the list 
>>>	print L1

Output

[1, 2, 5, 6]

6) Other functions & methods

i) insert( )

This list method allows us to insert a value,at the given position or at the specified index,and the remaining elements gets shifted to accommodate the new element. Insert method takes two arguments -indexand item.

Syntax

list. insert (index, item)

Example

>>>	L1= [29, 78, 45, 12, 1, 23]
>>>	L1.insert (3, 90)
>>>	print L1

Output

[29, 78, 45, 90, 12, 1, 23]

If the specified index is less than zero, the item is inserted at the beginning of the list and if the given index is greater than the length of the list , the item is inserted at the last.

Output

>>>	L1.insert(10, 2)
>>>	print L1
>>>	L1.insert(-2, 50)
>>>	print L1

Output

[29, 78, 45, 90, 12, 1, 23, 2]
[50, 29, 78, 45, 90, 12, 1, 23, 2]

ii) reverse()

This method can be used to reverse the elements of the list. It does not return anything.

Syntax

list.reverse ( )

Example

>>>	L1.reverse()
>>>	print L1

Output

[2, 23, 1, 12, 90, 45, 78, 29, 50]

iii) sort()

For arranging elements in a fashion, Python provides a method sort() and a function sorted(). sort() arrange or modifies the list in place and sorted() returns a new sorted list.

Syntax

list.sort ([cmp,[key,[reverse]]])

Parameters mentioned in [ ] are optional. These parameters allow us to customize the method.

1) Example

>>>	L1.sort ( )
>>>	print L1

Output

[1, 2, 12, 23, 29, 45, 50, 78, 90]

2) Example

>>>	L2= ["Ruby", "C", "Java","C++", "Python"]
>>>	L2.sort ( )
>>>	print L2

Output

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

3) Example

>>>	L2.sort (key=len)
>>>	print L2

Output

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








COMMENTS