Skip to content

Python lists, Tuples, Sets and Dictionaries

In the previous article we discussed what python is used for. In this article we’ll discover Python lists, so we are going to be looking at the difference between lists, tuple, set and dictionary.

Just Released: Python Projects For Beginners – Learn With Coding Example

Python Lists

Lists, tuple, set and dictionary are equivalent to collections in other programming languages. The question you might ask is what is the difference between them? why this is necessary is because there is so much similarity between these items especially for list, tuples and dictionaries.

The first thing we are going to do we are going to create them and then mention the differences between them. so let’s create a list which simply is a comma separated items enclosed in square braces:

list1 = ["Computer", "Printer", "TV", "Camera"]

The key word you need to take away from here is square braces [] . That is one thing you need to know about the list, is that it is enclosed in square braces.

Let’s focus on the differences and talk about the similarities. When you are creating a list, you can even put any other item of a different data type inside of it. It’s going to work.

list1 = ["Computer", "Printer", "TV", "Camera", 1989, 3.14]

If I go ahead and run this code, it is going to work. You can also go ahead to save and print this list, if I print it, you can see that it printed out to the output. it print out everything without issues even if there are multiple data types inside the list.

Python list example
Python list example

Python Lists vs Tuples

Let’s look at the other item and that will be Tuple. A tuple is also a list of items separated by comma. In fact let’s just copy the list and then make the changes that is going to make it into a tuple. So let’s just copy it, give it name tuple1 and enclosed in in rounded braces.

list1 = ("Computer", "Printer", "TV", "Camera", 1989, 3.14)

When enclosed in rounded braces the compiler understands that you are creating a tuple. The keyboard is rounded braces. This is a tuple and we can perform the same operation to print it. print (tuple1) is going to print exactly the same thing. Let’s go ahead and run it and see the output.

Python tuple example
Python tuple example

As you see, there is actually no difference between a list and a tuple, but I’m telling you that there is one key difference that is: a tuple is enclosed in rounded braces while a list is enclosed in square braces.

Another difference you need to know is that a tuple is immutable. and that means that if you create a tuple, you can not change the items in it.
Let’s change the first item in list1 from “Computer” to “PC”
To that simple call the first item in the list, that is list1[0] as element numbering in Python starts at 0, then assign the value “PC” to it.
You can do that like this: list1[0] = "PC"
now if you right print (list1) you will see that the first item is now “PC” instead of “Computer”.

Let’s do the same thing in a tuple and see if it is going to work. Let’s say tuple1[0] = "PC", this is actually not going to work, if you write print (tuple1) the interpreter will throw an error

immutable vs mutable
immutable – mutable

The reason why it didn’t work is because tuples are immutable.

You will also like: Python Dictionaries – Python Beginners Series

Python Lists vs Sets

Let’s look at the next item and that will be: Sets. The keyword in this case is curly braces. Python helps us to understand the difference between the items in the programming language very easily, and that is why we say that python is the easiest programming language to work with.

Now let’s create a set, this time let’s call it set1, but the syntax for creating this set is a bit different. In this case we will use the set keyword.

set1= set(["Computer", "Printer", "TV", "Camera", 1989, 3.14])

if we try to remove the internal braces, the interpreter will throw an error

python set example
python set example

The thing to remember here, is that the set is created using the set keyword and the braces (outer rounded braces and inner curly braces)

New to Python Programming? Get up and running with Python Basics in 10 minutes (Video).

Python Dictionaries

The next element we need to talk about is dictionary.
Dictionaries are a bit different and clear from all those other items.
Dictionary is created a bit differently because a Python dictionary is made of key-value pairs. think of it like the dictionary you’re using that’s made up of word-definition pairs

let’s create a dictionary to see how it works. we can do it like this
dict1 = {
1 : "Monday",
2 : "Tuesday",
3 : "Wednesday",
4 : "Thursday",
5 : "Friday",
6 : "Satudrday",
7 : "Sunday"
}

So, if you write print(dict1) you’re going to retrieve the values you stored in that dictionary.

python dictionary example
python dictionary example

NB. Please always respect the indentation when writing Python code

Another thing you need to know about dictionaries, is that the keys are immutable, just like a tuple elements. This means when you specified keys you can not change them. In addition to that, the dictionary’s keys must be unique, this means in this example we used “1” as a key for the value “Monday” so we are not allowed to use “1” as a key again in that same dictionary.

I hope this has been clear and that by now you know the differences between Python lists, sets, tuples, and dictionaries, learn more by enrolling in my Python online course. Please let me know if you have any, questions, remarks, or suggestions in the comment section and follow me on twitter.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)