sort() methodThe 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. Show ExampleLet'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]type() functionFor the type() function, it returns the class type of an object. ExampleHere we will see what type of both fam and fam2 are: Let's see what the type of the object is: type(fam) listNow, let's look at fam2. fam2 = [["liz", 1.73], ["emma", 1.68], ["mom", 1.71], ["dad", 1.89]] fam2 [['liz', 1.73], ['emma', 1.68], ['mom', 1.71], ['dad', 1.89]]Let's see what the type of the object is: type(fam2) listThese calls show that both fam and fam2 are in fact lists. append() methodThe append() method will add certain content you enter to the end of the elements you select. ExampleIn this example, let’s extend the string by adding “April” to the list with the method append(). Using append() will increase the length of the list by 1. months = ['January', 'February', 'March'] months.append('April') print(months)When you run the above code, it produces the following result: ['January', 'February', 'March', 'April']How to Compare Two Lists in Python using set(), cmp() and difference() FunctionsWhile working with lists in Python, you might have encountered two lists which seem similar. To figure out the difference, you have to compare the data items of both lists. You can do this by using the set(), difference() and sort() methods. In this article, we will understand how to compare two lists in Python. What is List and its function?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. sort(): Sorts the list in ascending order. Is a function a list?list is a type , which means it is defined somewhere as a class, just like int and float . So, list() is not a function. What are some of the methods and functions that can be used with lists?Python List/Array Methods
|