DEV Community

loading...
Cover image for What Are The Differences Between a List, Tuple, Dictionary & Set in Python? 🐍

What Are The Differences Between a List, Tuple, Dictionary & Set in Python? 🐍

arvindmehairjan profile image Arvind Mehairjan ・3 min read

Lists, tuples, dictionaries, and sets are all examples of collection data methods in Python. These data types are all different from each other and thus important to know which data-type to use for your code in order to improve the quality and efficiency of your code. Let's check them out.

Lists

A list is a collection where you can put your values using square brackets, which is ordered and changeable. You can put several data-types in a list, such as integers, strings, and booleans. An example of a list would look like this:

list_of_shoes = ["Adidas", "Reebok", "Nike"]
Enter fullscreen mode Exit fullscreen mode


To access a value within a list, you have to use index numbers. For example, if we want to access the second value in the list, we need to use the index number 1 (Computers counts from zero). To access a value within a list would look like this:

>>> list_of_shoes[1]
Reebok # Output
Enter fullscreen mode Exit fullscreen mode


You can also change the value within a list. For example, we want to change the third item on the list. We need to access it using an index number, which would look like this:

>>> list_of_shoes[2] = "Converse"
>>> list_of_shoes
list_of_shoes = ["Adidas", "Reebok", "Converse"] # output
Enter fullscreen mode Exit fullscreen mode

Tuples

A tuple is a collection data type that is ordered and not changeable. It uses parentheses where you can put in the values. An example of a tuple would look like this:

example_tuple = ("Banana", "Apple", "Grapes") 
Enter fullscreen mode Exit fullscreen mode


The major difference compared to a list is that a tuple is immutable, which means the value within the tuple cannot be changed, added or removed. If you try to change it, you will receive an error in Python.

>>> example_tuple = ("Banana", "Apple", "Grapes") 
>>> example_tuple[2] = ("Pear") 
Traceback (most recent call last):
  File "<pyshell#420>", line 1, in <module>
    example_tuple[2] = ("Pear") 
TypeError: 'tuple' object does not support item assignment
Enter fullscreen mode Exit fullscreen mode

Sets

A set is a collection data type that is unordered and unindexed and uses curly braces. This means you do not know in what order the data is and thus you cannot access it using an index number. A set looks like this:

set_of_games = {"Battlefield", "Fortnite", "Warzone"}
Enter fullscreen mode Exit fullscreen mode


To access a set, you have to use a for loop, like the example below:

>>> set_of_games = {"Battlefield", "Fortnite", "Warzone"}
>>> for x in set_of_games:
>>> print(x)
Battlefield
Fortnite
Warzone
Enter fullscreen mode Exit fullscreen mode


A value in a set cannot be changed but new items can be added or removed. You can use the add() method and remove().

Dictionaries

A dictionary is a collection data type that is unordered and indexed and can be changed and uses curly braces. It uses keys and values for the structure of the data. The key is the input of the data and the value is the actual data you are using. A dictionary looks like this:

my_dictionary = {
  "age": 24,
  "location": "Tokyo",
  "favorite_food": "ramen"
}
Enter fullscreen mode Exit fullscreen mode


You can access a value within a dictionary using keys.

>>> my_dictionary["age"]
24 # Output
Enter fullscreen mode Exit fullscreen mode


Lastly, you can change the values in dictionaries.

You can access a value within a dictionary using keys.

>>> my_dictionary["age"] = 31
>>> my_dictionary["age"]
31 # Output
Enter fullscreen mode Exit fullscreen mode

Conclusion

These are the main differences between these collection data types in Python. I hope you have a better understanding of these data types and which one to use for your application. If you want to check out more data types, I made a YouTube video that explains more different data-types.

Discussion (2)

pic
Editor guide
Collapse
wiwiwjb profile image
Wiwiwj bwjwjsihsnwnix

thanks. Python is awesome.

Collapse
arvindmehairjan profile image
Arvind Mehairjan Author

Thanks! Yeah, Python is an awesome programming language to use!