Home » Python » Python programs

Python program for various list operations

Python List operations example: Here, we are going to learn about the various list operations like accessing elements, printing elements, reversing elements, inserting, etc. in Python.
Submitted by IncludeHelp, on August 06, 2019

Here, we are implementing a python program for various list operations, following operations are being performed in the list,

  1. Declaring an integer list
  2. Printing the complete list
  3. Printing the first element of the list
  4. Printing ith element of the list
  5. Printing elements within the given indexes
  6. Printing a specific element using negative indexing
  7. Appending an element to the list
  8. Finding the index of a specific element in the list
  9. Sorting the list elements
  10. Popping an element from the list
  11. Removing specified element from the list
  12. Inserting an element at specified index
  13. Extending the list i.e. insert set of element (list) in the list
  14. Reversing list elements

Python code for various list operation

# Python code for various list operation

# declaring a list of integers
iList = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

# List slicing operations
# printing the complete list
print('iList: ',iList)
# printing first element 
print('first element: ',iList[0]) 
# printing fourth element 
print('fourth element: ',iList[3]) 
# printing list elements from 0th index to 4th index
print('iList elements from 0 to 4 index:',iList[0: 5]) 
# printing list -7th or 3rd element from the list
print('3rd or -7th element:',iList[-7]) 


# appending an element to the list
iList.append(111)
print('iList after append():',iList)

# finding index of a specified element 
print('index of \'80\': ',iList.index(80))

# sorting the elements of iLIst
iList.sort()
print('after sorting: ', iList);

# popping an element
print('Popped elements is: ',iList.pop())
print('after pop(): ', iList);

# removing specified element
iList.remove(80)
print('after removing \'80\': ',iList)

# inserting an element at specified index 
# inserting 100 at 2nd index 
iList.insert(2, 100)
print('after insert: ', iList)

# counting occurances of a specified element
print('number of occurences of \'100\': ', iList.count(100))

# extending elements i.e. inserting a list to the list
iList.extend([11, 22, 33])
print('after extending:', iList)

#reversing the list
iList.reverse()
print('after reversing:', iList)

Output

iList:  [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]  
first element:  10
fourth element:  40  
iList elements from 0 to 4 index: [10, 20, 30, 40, 50]
3rd or -7th element: 40 
iList after append(): [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 111] 
index of '80':  7 
after sorting:  [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 111] 
Popped elements is:  111
after pop():  [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]  
after removing '80':  [10, 20, 30, 40, 50, 60, 70, 90, 100] 
after insert:  [10, 20, 100, 30, 40, 50, 60, 70, 90, 100]
number of occurences of '100':  2
after extending: [10, 20, 100, 30, 40, 50, 60, 70, 90, 100, 11, 22, 33] 
after reversing: [33, 22, 11, 100, 90, 70, 60, 50, 40, 30, 100, 20, 10] 





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




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


© https://www.includehelp.com some rights reserved.