Is list and tuple same in Python?
This is an example of Python lists: my_list = [0,1,2,3,4] top_rock_list = ["Bohemian Rhapsody","Kashmir","Sweet Emotion", "Fortunate Son"]This is an example of Python tuple: my_tuple = (a,b,c,d,e) celebrity_tuple = ("John", "Wayne", 90210, "Actor", "Male", "Dead")Python lists and tuples are similar in that they both are ordered collections of values. Besides the shallow difference that lists are created using brackets "[ ... , ... ]" and tuples using parentheses "( ... , ... )", the core technical "hard coded in Python syntax" difference between them is that the elements of a particular tuple are immutable whereas lists are mutable (...so only tuples are hashable and can be used as dictionary/hash keys!). This gives rise to differences in how they can or can't be used (enforced a priori by syntax) and differences in how people choose to use them (encouraged as 'best practices,' a posteriori, this is what smart programers do). The main difference a posteriori in differentiating when tuples are used versus when lists are used lies in what meaning people give to the order of elements. For tuples, 'order' signifies nothing more than just a specific 'structure' for holding information. What values are found in the first field can easily be switched into the second field as each provides values across two different dimensions or scales. They provide answers to different types of questions and are typically of the form: for a given object/subject, what are its attributes? The object/subject stays constant, the attributes differ. For lists, 'order' signifies a sequence or a directionality. The second element MUST come after the first element because it's positioned in the 2nd place based on a particular and common scale or dimension. The elements are taken as a whole and mostly provide answers to a single question typically of the form, for a given attribute, how do these objects/subjects compare? The attribute stays constant, the object/subject differs. There are countless examples of people in popular culture and programmers who don't conform to these differences and there are countless people who might use a salad fork for their main course. At the end of the day, it's fine and both can usually get the job done. To summarize some of the finer details Similarities:
Differences: - A priori, by definition
Differences - A posteriori, in usage
List and Tuple are built-in container types defined in Python. Objects of both these types can store different other objects that are accessible by index. List as well as tuple is a sequence data type, just as string. List as well as tuple can store objects which need not be of same type. List : A List is an ordered collection of items (which may be of same or different types) separated by comma and enclosed in square brackets. In [1]: L1=[10,25.5,3+2j,"Hello"] L1 Out[1]: [10, 25.5, (3+2j), 'Hello']In above list, each item is of different type. Further, each item is accessible by positional index starting from 0. Hence L1[2] will return 25.5 In [2]: L1[1] Out[2]: 25.5Tuple: Tuple looks similar to list. The only difference is that comma separated items of same or different type are enclosed in parentheses. Individual items follow zero based index, as in list or string. In [3]: T1=(10,25.5,3+2j,"Hello") T1 Out[3]: (10, 25.5, (3+2j), 'Hello') In [4]: T1[1] Out[4]: 25.5Difference between List and Tuple:The obvious difference is the use of square brackets [] in List and parentheses () in tuple as enclosures. However, the important difference is that List as a mutable object and Tuple is an immutable object. If contents of an object can be modified in place, after it has been instantiated, is a mutable object. On the other hand, any operation on immutable object that tries to modify its contents is prohibited. In above example, any item in the list L1 can be assigned with different value. Let us change value of item at index=2 from 3+2j to 1.22E-5 In [5]: L1[2]=1.22E-5 L1 Out[5]: [10, 25.5, 1.22e-05, 'Hello']The built-in List class has different methods that allow various operations on List object (such as insertion, deletion, sorting etc) However, any such operation is not possible with Tuple object. If we try to modify T1 by changing value of item at index=2 to 1.22E-5, TypeError exception is raised. Following built-in functions can be used along with List as well as Tuple.
If items in list/tuple are strings, min() and max() functions returns string that comes first/last in alphabetical order. If list/tuple is made up of numeric and nonnumeric values, TypeError exception is raised as comparison of dissimilar objects is not possible. In [8]: L2=['pen', 'book','computer', 'table', 'file'] T2=('pen', 'book','computer', 'table', 'file') print ('max of L2', max(L2)) print ('max of T2', max(T2)) print ('min of L2', min(L2)) print ('min of T2', min(T2)) max of L2 table max of T2 table min of L2 book min of T2 book Out [9]: L3=[100, "hundred", 0.001] print ('max of L3', max(L3)) --------------------------------------------------------------------------- TypeError Traceback (most recent call last)The built-in list class has following methods to perform various operations on list object. Following methods allow new items to be added to list.
Following methods are used to remove items from given list.
Following methods rearrange sequence of items in the list
If you recall, tuple is an immutable object. Hence the tuple class doesn’t have similar methods perform insertion, deletion or rearrangement of items. Conversion functionsAll sequence type objects (string, list and tuple) are inter-convertible. Python’s built-in functions for this purpose are explained below:
When a list or tuple is converted to string by str() function, the string representation is not exactly similar to word, but list or tuple surrounded by single quote marks. To form a continuous sequence of characters in the list, use join() method of string object. In [17]: L2=['H', 'e', 'l', 'l', 'o'] s1=str(L2) s1 Out[17]: "['H', 'e', 'l', 'l', 'o']" In [18]: s2="".join(L2) print ('string from list items:', s2) string from list items: HelloIn this chapter, we discussed the list and tuple objects, their functions and methods. In next chapter we shall learn about dictionary data type. |