Tuple¶
Just like Lists, Tuple is also used to store multiple items in a variable.
Differences between Tuple and List¶
Tuple | List |
---|---|
Tuple is Immutable | List is Mutable |
Tuple is more appropriate for accessing data | List is more appropriate for performing insertion, updating and deletion |
Tuple consumes Less memory compared to Lists | Lists consume more memory compared to Tuple |
Operations on Tuple is Less time consuming | List operations are time consuming |
Preferred over Lists to store Heterogenous items | Preferred over Tuple to store Homogeneous items |
Supports less in-built functions | Supports many in-built functions |
Initialized using () | Initialized using [] |
Similarities between Tuple and List¶
- Both of them are in-built data types.
- Both of them are used to store multiple items in a single variable.
- Both of them can hold duplicate items.
- Both List and Tuple items can be accessed using their index.
In-built methods¶
Python supports below in-built methods for Tuples.
- count()
- index()
count()¶
count method is used to identify the number of times an item occurs in a tuple.
index()¶
index method is used to retrieve the index of a specific item in a list. If the specific item is present multiple times in a list, index method returns the index of first match.