Home » Python

List Operations in Python - I

Learn: In this article, we will going to discuss about various list operations as accessing, traversing, list slicing, appending list elements with various examples and brief explanation.
Submitted by Abhishek Jain, on October 01, 2017

1) Accessing an element of list

For accessing an element, we use index.Let’s access elements of a list.

>>> List =[1,2,3,[6,7,8],4]

>>>print List [3]
[6, 7, 8]

To access an element of list containing another list, we use pair of index. Also a sub-list of list can be accessed using list slice.

As the 4th element of this List is another list. To access elements of/from this sub-list, we will use...

>>>print List [2] [0]
>>>print List [2] [2]

List Slices

Slice operator works on list also. We know that a slice of a list is its sub-list. For creating a list slice, we use [l:r] operator. This will inclusive for lth element but exclusive for rth element (including the first element but excluding the last element). So the resultant list will have m-n elements in it.

>>> List1 [1:2] 
will give

Slices are treated as boundaries, and the result will contain all the elements between boundaries.

Its Syntax is:

seq = L [start: stop: step]

Where start, stop & step- all three are optional. If you omit first index, slice starts from "0" and omitting of stop will take it to end. Default value of step is 1.


For list L2 containing ["Gwalior", "Bhopal", "Indore"]
>>>L2 [0:2]
["Gwalior", "Bhopal"]


>>>list = [11,12, 13, 14, 15, 16]

>>>list [::2] # produce a list with every alternate element
[11, 13, 15]
>>>list [4:] # will produce a list containing all the elements from 5th positiontill end[15, 16]
>>>list [:3]
[11, 12, 13]
>>>list [:]
[11, 12, 13, 14, 15, 16]
>>>list [-1]  # "-1"  refers to last elements of list

2) Traversing a List

Let us visit each element (traverse the list) of the list to display them on screen. This can be done in many ways:


i) for i in List

print i

will produce following output
2 4 6 8 10

ii) i= 0

L = len (List)
while i < L :
print List [i],
i + = 1

will produce the same output.

(iii) for i in range ( len (List))

print List [i]

will produce the same output.

Using 2nd way for transversal, we are only able to access the list, but other ways can alsobe used to update or write the element to the list.

3) Appending in the list

Appending a listmeans adding more elements at the end of the list. To add new elements at the end of the list, Python provides:

i) append ( )

append () method add a new element at the end of the list.

Its Syntax is:

List.append (item)


>>>List.append(5)   # this will add 5 to the list at the end
>>>print List
>>>print len(List)  # print length of the list List

will produce following output

[1, 2, 3, 4, 5]

ii) extend()

Using append ( ), only one element at a time can be added. For adding more than one element, extend ( ) method can be used.It can also be used to add elements of another list to the existing one.

>>>List=[1 ,2 ,3 ,4]
>>>List1 = ['x', 'y', 'z']
>>>List. extend (List1)
>>>print List

will produce following output

[1, 2, 3, 4, 'x', 'y', 'z']


Comments and Discussions!





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.