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? 🐍

helloiamarra profile image Arra ・3 min read

Complete Overview of Basic Programming Concepts using Python (2 Part Series)

1) Complete Overview of Basic Programming Concepts using Python - Installation, Syntax, Indentation, Comments & Variables (Part 1) 2) What Are The Differences Between a List, Tuple, Dictionary & Set in Python? 🐍

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"]


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


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

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") 


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

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"}


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


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"
}


You can access a value within a dictionary using keys.

>>> my_dictionary["age"]
24 # Output


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

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.

Complete Overview of Basic Programming Concepts using Python (2 Part Series)

1) Complete Overview of Basic Programming Concepts using Python - Installation, Syntax, Indentation, Comments & Variables (Part 1) 2) What Are The Differences Between a List, Tuple, Dictionary & Set in Python? 🐍

Posted on May 12 by:

helloiamarra profile

Arra

@helloiamarra

I am software tester who build things, break things, test things and fix things.

Discussion

markdown guide
 
 

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