Best post on List methods in python – 3

List methods in python

Python supports various compound datatypes like List, tuple,dictionary etc.

In previous post we studied about how lists are mutable and what are list operations?

In this post we will be concentrating on various list methods.

List consist of various methods for easy functioning and utilization of python script.

Many list methods like list(), append(), remove() pop() len() etc are used to provide various functionalities and can be used again and again as per need without need of coding again this helps in reducing redundancy. 

List of list methods

  • append()
    • It appends an element at the end of the list.
    • L.append(ele)
  • count()
    • It returns the number of occurrences of value in the list.
    • L.count(ele) 
  • clear()
    • It clears all the elements or values present in the list without deleting entire list.
    • L.clear()
  • copy()
    • It copies elements from one list to another list.
    • List mentioned in parenthesis is copied in original list. 
    • L.copy(L2)

       

  • extend()
    • It extends list by appending elements from the iterable.
    • In short we can add more than one value in the end of the original list by using extend method.
    • L.extend(L2)
  • index()
    • It return first index of value and raises ValueError if the value is not present.
    • L.index(value, [start, [stop]])
  • insert()
    • It inserts new element on desired index in the given list.
    • When the value is added on given index, it will not replace existing value by deleting it instead it will get added to the desired location by shifting remaining values further.
    • L.insert(index, object)
  • pop()
    • It adds new element in the end of the given list if no parameters are mentioned.
    • If we provide the parameter i.e. index value , it will delete the value from given index.
    • L.pop()
      OR
      L.pop(index)
  • remove()
    • It removes first occurrence of value and raises ValueError if the value is not present.
    • NOTE: value mentioned in remove() is not the index position it is the actual value. 
    • In case of repetitive values the first occurrence of value will be deleted.
    • L.remove(value)
  • reverse()
    • It reverses the given list.
    • L.reverse()
  • sort()
    • It sorts the given list in ascending order by default.
    • If we wish to sort in ascending order we can provide a parameter in sort() called as reverse.
    • By default reverse = False, if we want result in descending order we must set reverse=True
    • L.sort() #ascending order
      L.sort(reverse=True) #descending order

  • len()
    • It returns the length of the list
    • L.len()
  • list()
    • It is used for type casting i.e. it is used to convert other datatypes to list datatype.
    •  list(seq)
  • max()
    • It is used to find max value from the list
    • max(L)
  • min()
    • It is used to find min value from the list
    • min(L)

Example of list methods

#LIST METHODS IN PYTHON (itvoyagers.in)
var1=['a','python',2,6.3]

print('var1 is',var1)
var2=[6,5,4,'l','program']
print('var2 is',var2)
print("n ")

#length of list

print('len(var2)',len(var2))
print('len(var1)',len(var1))
print("n ")

#type function

print('type(var2)',type(var2))
print('type(var1)',type(var1))
print("n ")

#appending values

var1.append(5)
print(' after appending value in var1',var1)
var1.insert(2,'new')
print('after inserting value in var1',var1)
var1.extend(var2)
print('after extinding iterable in var1',var1)
print("n ")

#count

x=var1.count(5)
print("count of var1",x)
print("n ")

#copy

var5=var1.copy()
print("var5",var5)
print("n ")

#reverse

var1.reverse()
print('reverse of var1 is',var1)
print("n ")

#sorting

var3=[2,5,6,7,8,1,0,9]
print('befor sorting var3',var3)
var3.sort() #for ascending order
print('sorted var3 in ascending order',var3)
var3.sort(reverse=True) #for descending order
print('sorted var3 in descending order',var3)
print("n ")

#repetition operator

print("var1*3",var1*3)
print("n ")

#concatenation

print("var1+var3",var1+var3)
print("n ")

#clear values

var5.clear() #clears all elements but does not delete list
print("Cleared var5",var5)
print("n ")

#deleting values from list

print("var1 before del statement",var1)
del var1[3] #del statement 
print('var1',var1)
var2.remove(5) #remove() takes value as parameter
print('after removing value from var2',var2)
var2.pop() #removes last element from list
print('after poping last value from var2',var2)
var2.pop(1) #pop() can take index as parameter
print('after poping value from given index in var2',var2)

 

 

 

 

 

Output of list methods in python

list methods (itvoyagers.in)

Other python related post

Best post on programming language and its types – 1
Python Programming (Basics) – History & Features
Python Programming (Basics)- Debugging & Types of errors
Python Programming (Basics) – Variables, Values, Types & Keywords.
Best post on built-in functions in python – 1
Python Programming (Basics) – Type Conversion
Python Programming (Basics) – Comments,Indentation,Built-in Number Data types and Expressions
Best post on IDLE & script mode in Python – 1
Python Programming (Basics)- Operators in Python
Best post on Order of Operations – Python
Simple and compound statements of python in easy way
Best post on conditional statements in python – Part 1
Best post on conditional statements in python – Part 2
Best post on looping in python (for loop)-Part 1
Best post on looping in python (while loop)-Part 2
Best post on nested loop in python(looping) -Part 3
Best post on infinite loop in python(looping) -Part 4
Best post on control statements(break,continue,pass)-1
Best post on Function definition & calling in python -1
Easy flow of function execution,parameters,arguments-2
Easy fruitful & void function,return values,stack diagrams-3
Best post on types of function arguments in python- 4
Best post on recursive function, scope of variable-5
Best post on import and from statement in python – 1
Best post on modules and built-in modules math,random & time-2
Best post on user defined modules in python-3
Best post on string datatype in python – 1
Best post immutable string,string operations python-2
Best post on string methods in python – 3
Best post on list datatype in python – 1
Best post on mutable list, list operations python – 2
Best post on List methods in python – 3
Best post on dictionary datatype in python – 1
Best post on dictionary methods and operations-2
Best post on tuple datatype in python – 1
Best post on tuple operations and immutable tuple- 2
Best post on tuple methods and built-in functions-3
17 -Python program to demonstrate Button in tkinter and its event in easy way
New posts coming soon…….

Other advance python related post

File Systems and File Handling in Python
Types of file modes and attributes of file object in Python
How to read,write and append in a file in python?
Remaining file methods in Python
File Positions in Python
Directory in Python and its methods
Iterator and Iterables in Python
Exceptions in Python
Exception Handling in Python (Part I)
Exception Handling in Python (Part II)
Regular Expressions
Metacharacters or Regular Expression Patterns
Functions in ‘re’ module(Part I)- Match vs Search
Functions in ‘re’ module(Part II)-findall(), split(), sub()
Flags for regular expressions(Modifiers)
GUI programming in Python and Python GUI Library
What is Tkinter ?
Layout Manager (Geometry Manager) in Python
Events and Bindings in Python along with Widget configuration and styling
Fonts Names, Font Descriptors, System Fonts, Text formatting, Borders, Relief Styles in Python
Best post:Dimensions, Anchors, Bitmaps & Cursors in Python
Canvas widget of tkinter module – Python
Widgets in tkinter module – Python
Label, Text, Entry & Message Widget in Python
Button, Checkbutton & Radiobutton Widget in Python
Best post on Menu and Menubutton Widget of tkinter
Best post- Listbox and Scrollbar(Slider) Widget-Python
Best post on Frame Widget of tkinter in Python
Best post: message box widget and its methods
Best post- LabelFrame, Toplevel, PanedWindow widgets
Best post on Spinbox and Scale Widget in Python
Best post : Database connectivity in Python (Part 1)
Best post : Database Connectivity in Python (Part 2)
Best post on Create and Insert query : Python + MySQL
Best post on Select Query to Search & Read: Python + MySQL-4
Best post on Update query and Delete query : Python + MySQL-4
New posts coming soon…….
We are aiming to explain all concepts of Python in easiest terms as possible.
ITVoyagers-logo
ITVoyagers
Author

Leave a Comment