Home
Problem
Status
Contest
Workbook
User
Group
Forum
Register
Login
{"managingGroups":{},"author":"FolkN","updateTime":1712374372000,"title":"List and set","dislikeCnt":0,"content":"\nIn 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.\n\nWhile 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.\n\nPython offers the following list functions:\n\nsort(): Sorts the list in ascending order.\ntype(list): It returns the class type of an object.\nappend(): Adds a single element to a list.\nextend(): Adds multiple elements to a list.\nindex(): Returns the first appearance of the specified value.\nmax(list): It returns an item from the list with max value.\nmin(list): It returns an item from the list with min value.\nlen(list): It gives the total length of the list.\nlist(seq): Converts a tuple into a list.\ncmp(list1, list2): It compares elements of both lists list1 and list2.\nfilter(fun,list): filter the list using the Python function. \nPython List Functions \u0026 Methods\nPython sort list method\nThe 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.\n\nExample\nLet\u0027s 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.\n### Lists:\n1. **Definition:** Lists are ordered, mutable collections of objects in Python. They can contain elements of different data types and are defined using square brackets `[]`.\n\n2. **Creating a List:**\n - Example: `my_list \u003d [1, 2, 3, \u0027hello\u0027, True]`\n\n3. **Accessing Elements:**\n - Elements can be accessed using index numbers.\n - Indexing starts from 0.\n - Example: `my_list[0]` returns the first element.\n\n4. **List Methods:**\n - `append(element)`: Adds an element to the end of the list.\n - `insert(index, element)`: Inserts an element at the specified index.\n - `remove(element)`: Removes the first occurrence of the specified element.\n - `pop(index)`: Removes and returns the element at the specified index.\n - `index(element)`: Returns the index of the first occurrence of the specified element.\n - `sort()`: Sorts the list in ascending order.\n - `reverse()`: Reverses the order of elements in the list.\n - `len(list)`: Returns the number of elements in the list.\n - `count(element)`: Returns the number of occurrences of the specified element.\n\n### Sets:\n1. **Definition:** Sets are unordered collections of unique elements in Python. They are defined using curly braces `{}`.\n\n2. **Creating a Set:**\n - Example: `my_set \u003d {1, 2, 3, 4}`\n\n3. **Set Operations:**\n - `add(element)`: Adds an element to the set.\n - `remove(element)`: Removes the specified element from the set.\n - `discard(element)`: Removes the specified element from the set if it exists.\n - `pop()`: Removes and returns an arbitrary element from the set.\n - `clear()`: Removes all elements from the set.\n - `union(set1, set2)`: Returns a new set containing elements from both sets.\n - `intersection(set1, set2)`: Returns a new set containing elements that are common in both sets.\n - `difference(set1, set2)`: Returns a new set containing elements that are in set1 but not in set2.\n - `symmetric_difference(set1, set2)`: Returns a new set containing elements that are in either of the sets, but not both.\n\n4. **Set Methods:**\n - `len(set)`: Returns the number of elements in the set.\n - `copy()`: Returns a shallow copy of the set.\n - `isdisjoint(set)`: Returns True if two sets have no common elements.\n - `issubset(set)`: Returns True if all elements of the set are present in another set.\n - `issuperset(set)`: Returns True if all elements of another set are present in the set.\n\nLists are ordered and allow duplicate elements, while sets are unordered and contain only unique elements. Lists are suitable for scenarios where order and duplicates matter, while sets are ideal for operations that require unique elements or set operations like union, intersection, etc.","threadId":188784,"likeCnt":18,"createTime":1712374372000,"isWorkbook":false,"viewCnt":478,"openness":2,"fav":false,"id":4820,"trustable":false}