In Python, you can use a list() function which creates a collection that can be manipulated for your analysis. This collection of data is called a list object.
While all methods are functions in Python, not all functions are methods. There is a key difference between functions and methods in Python. Functions take objects as inputs. Methods in contrast act on objects.
Python offers the following list functions:
sort(): Sorts the list in ascending order.
type(list): It returns the class type of an object.
append(): Adds a single element to a list.
extend(): Adds multiple elements to a list.
index(): Returns the first appearance of the specified value.
max(list): It returns an item from the list with max value.
min(list): It returns an item from the list with min value.
len(list): It gives the total length of the list.
list(seq): Converts a tuple into a list.
cmp(list1, list2): It compares elements of both lists list1 and list2.
filter(fun,list): filter the list using the Python function.
Python List Functions & Methods
Python sort list method
The sort() method is a built-in Python method that, by default, sorts the list in ascending order. However, you can modify the order from ascending to descending by specifying the sorting criteria.
Sort a list in ascending order
Let's say you want to sort the element in prices in ascending order. You would type prices followed by a . (period) followed by the method name, i.e., sort including the parentheses.
prices =[238.11,237.81,238.91]
prices.sort()print(prices)# [237.81, 238.11, 238.91]Работает на
Редактор с примерами для обучения, нажмите Tab для ввода. Нажмите Escape, затем Tab или Shift+Tab для навигации.
In Python, you can use a list() function which creates a collection that can be manipulated for your analysis. This collection of data is called a list object.
While all methods are functions in Python, not all functions are methods. There is a key difference between functions and methods in Python. Functions take objects as inputs. Methods in contrast act on objects.
Python offers the following list functions:
Python sort list method
The
sort()method is a built-in Python method that, by default, sorts the list in ascending order. However, you can modify the order from ascending to descending by specifying the sorting criteria.Sort a list in ascending order
Let's say you want to sort the element in prices in ascending order. You would type prices followed by a . (period) followed by the method name, i.e.,
sortincluding the parentheses.Python list type() function
For the
type()function, it returns the class type of an object.Check the data type of a list
Here we will see what type of both fam and fam2 are: