Computer Science
What is the difference between a list and a tuple ?
Python Tuples
4 Likes
Answer
List | Tuple |
---|---|
Lists are mutable sequences of Python i.e., we can change elements of a list in place. | Tuples are immutable sequences of Python i.e., we cannot change elements of a tuple in place. |
The syntax to create list is <list-name> = [value,.....] | The syntax to create tuple is <tuple-name> = (value, ....) |
Lists cannot be used as keys in dictionary. | Tuples can be used as keys in dictionary. |
Lists cannot be used as elements of a set. | Tuples can be used as elements of a set. |
Lists are slower compared to tuples. | Tuples are faster compared to lists. |
Answered By
3 Likes
Related Questions
What are the two ways to add something to a list ? How are they different ?
What are the two ways to remove something from a list? How are they different ?
In the Python shell, do the following :
- Define a variable named states that is an empty list.
- Add 'Delhi' to the list.
- Now add 'Punjab' to the end of the list.
- Define a variable states2 that is initialized with 'Rajasthan', 'Gujarat', and 'Kerala'.
- Add 'Odisha' to the beginning of the list states2.
- Add 'Tripura' so that it is the third state in the list states2.
- Add 'Haryana' to the list states2 so that it appears before 'Gujarat'. Do this as if you DO NOT KNOW where 'Gujarat' is in the list. Hint. See what states2.index("Rajasthan") does. What can you conclude about what listname.index(item) does ?
- Remove the 5th state from the list states2 and print that state's name.
Discuss the utility and significance of tuples, briefly.