Versus-Versus: List vs Tuple vs Dictionary
Lists, Tuples and Dictionary are some very unique features that Python has to offer, so today we will discuss about them and their differences.
Basic Definitions
- An ordered and changeable collection in python that allows duplicate members is called a List.
- An ordered and unchangeable collection in python that allows duplicate member is called a Tuple.
- An un-ordered but indexed and changeable collection in python that does not allow duplicate members is called a Dictionary.
How are they written?
The lists are written within square brackets. For example:
nelist = ["apple", "banana", "cherry"]
print(nelist)
Which gives an output like:
The tuples are written within round brackets. For example:
netuple = ("apple", "banana", "cherry")
print(netuple)
print(netuple)
Which gives an output like:
The dictionaries are written within curly brackets. They have key-value pair. For example:
nedict = {"a": "Apple",
"b": "Boy",
"c": 1234}
print(nedict)
"b": "Boy",
"c": 1234}
print(nedict)
Which gives an output like:
How to access their items?
We can access a list item by referring to the index number inside square brackets just as we referred to the index number for accessing array elements in C. For example:
nelist = ["apple", "banana", "cherry"]
print(nelist[2])
Which gives an output like:
We can access a tuple item in the same way as we access a list item. For example:
netuple = ("apple", "banana", "cherry")
print(netuple[1])
Which gives an output like:
A dictionary element can be accessed by typing the key name of that particular element inside square brackets. For instance:
nedict = {"a": "Apple",
"b": "Boy",
"c": 1234}
print(nedict["a"])
The obtained output is:
How do we change the item values?
A list item can be changed by simply accessing it and assigning it a new value.
A Tuple item as previously stated cannot be changed.
A dictionary item can be changed by accessing it as above and assigning it a new value.
The assignment of new values to the list as well as the dictionary item can be done by the assignment or '=' operator.
The length
The length of all the three list, tuple and dictionary can be seen by the len() method.
How to append/pop an Item?
- For a list:
- To add an item at the end, we use the append() method whereas to add an item at a specified index, we use the insert() method.
- There are several methods for removing items from a list:
- Use the remove() method for removing specified items.
- Use the pop() method to remove item at a particular index or the last item.
- Use the del keyword to remove a particular index. We can also delete the complete list using the del keyword.
- Using the clear() method will empty the list.
- For a tuple:
- Tuples are unchangeable. We cannot add items to them.
- We cannot remove items from a tuple.
- Although, we can use the del keyword to delete the tuple completely.
- For a Dictionary:
- To add a new item we use a new key and assign a value to it. In the following manner:
nedict = {"a": "Apple",
"b": "Boy",
"c": 1234}
nedict["d"]="School"
print(nedict)
The output is:
- There are several methods for removing items from a Dictionary:
- Use the pop() method to remove item with a particular key name.
- Use the popitem() method to remove the last inserted item.
- Use the del keyword to remove an item with a particular key name. We can also delete the complete dictionary using the del keyword.
- Using the clear() method will empty the dictionary.
Now, the Difference between the three
A list is ordered so that we can index into the list, or iterate over the list. List is mutable i.e. lists can be modified after they have been created. A tuple is similar to a list except it is immutable. Tuples have structure, lists have order. A dictionary has a key-value pair. Dictionary is comparatively faster than a list.
Comments
Post a Comment