
It stores data in the form of key-value pairs. DictionaryĪnother type of built-in data structure in Python is the dictionary. It allows retrieving a particular portion of the sequence. Each of them supports the slicing operation. Python provides support for three types of sequences lists, strings, and tuples. The latter allows directly fetching an item(s) from the sequence. The most distinguishing features of a sequence are membership tests and indexing operations. Lists and tuples qualify to be called as sequences, just like strings.
tuple () – Converts some list into a tuple.
min () – Returns the smallest value from a tuple.
max () – Returns the biggest value from a tuple. len () – Gives out the total length of some tuple. cmp () – Compares elements of two tuples. Like strings, tuples can be concatenated and sliced. Similar to string indices, tuple indices start at 0. So, a tuple, say singleton_tuple, with a single item, say item 1, is defined as: singleton_tuple = (item 1, ) This is done in order to allow Python to differentiate between a tuple and a pair of parentheses surrounding the object in some expression. You need to specify the same using a comma following the only item. For example, empty_tuple = ()ĭefining a tuple with a single item is a bit tricky. In order to define an empty tuple, an empty pair of parentheses is used. Tuples are used in scenarios where it is certain that the (set of) values belonging to some statement or a user-defined function will not change. Some_tuple = (item 1, item 2, item 3,…., item n) Though adding parentheses to the tuple is optional, its use is recommended to clearly define the end and start of the tuple. Typically, a tuple in the Python programming language is defined using parentheses with each item separated by commas. The most important difference between a list and a tuple is mutability. However, it doesn’t support the same level of extensive functionality. Similar to a list, the tuple is a built-in data structure in Python. sort () – Sort all elements of a list in the ascending order Tuple. reverse () – Reverses the order of all elements of the list. remove () – Eliminates an element from the list. pop () – Eliminates and returns an element from the list. insert () – Inserts an element to the list at the defined index. index () – Returns the index of an element (Note: If the same element appears multiple times in the list then the index of the very first match is returned). extend () – Adds all elements of a list to some other list. count () – Returns the total number of items passed as an argument. copy () – Returns a shallow copy of the list. clear () – Removes all elements from the list.
append () – Adds an element to the end of the list. Following are the various methods supported by lists in Python: A class can have fields, which are variables defined to use with an instance of that class only. Here some_list is the name of the list and some item is the element to be added to the list. The general syntax for the method is: some_list.append(‘some item’) For example, the append () method allows adding an item to the end of a list. The list is, therefore, also a type of class in Python.Īny class can have methods, which can be used only when we have an object of that specific class. For instance, assigning a value, say 22, to an integer variable, let it be i, is creating an object of the class integer. A list in the Python programming language is an example of using objects and classes.