What is the difference between list and tuple ? Give an example.
Answers
Answered by
2
Tuple is also a sequence data type that can contain elements of different data types, but these are immutable in nature. In other words, a tuple is a collection of Python objects separated by commas.
Answered by
2
List and Tuple in Python are the class of data structure.
Explanation:
The following are key differences between List and Tuple.
- The list is dynamic, while the tuple is static.
- The Lists are mutable whereas tuple is immutable.
- The implication of iterations is Time-consuming in Lists, whereas the implication of iterations is comparatively faster in a tuple.
- The list is better for performing operations, such as insertion and deletion, whereas tuple data type is appropriate for accessing the elements.
- The Lists consume more memory, whereas tuple consume less memory as compared to the list
- The Lists have several built-in methods, whereas tuple doesn't have built-in methods.
- The unexpected changes and errors are more likely to occur in Lists, whereas in tuple it is hard to take place.
List example:
- colors = ['red', 'blue', 'green']
- colors[1]='yellow'
- print(colors)
- Return: ['red', 'yellow', 'green']
Tuple example :
- colors = ('red', 'blue', 'green')
- colors[1]='yellow'
- print(colors)
- TypeError: 'tuple' object does not support item assignment
Similar questions
Chemistry,
5 months ago
Computer Science,
11 months ago
Computer Science,
11 months ago
Accountancy,
1 year ago
Science,
1 year ago