Differences-between-List-Tuple-Set-and-Dictionary-in-Python-300x147

Difference Between List, Tuple, Set and Dictionary in Python

We are going to create these various collections data types in Python and also outline the differences between them. Find the Video Lesson here

    1. Python List
    2. Python Tuple
    3. Python Dictionary
    4. Python Set

1. Python List

A list in python is a collection of various items  which may be either of the same or of different data types.  The items in a list are separated by commas and enclosed in square braces. Listing 1.0 shows how to create a list in python

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

scores = [56, 45.9, 89.5, 70, 32.9, 67.4]

letters = ['k', 'i', 'n', 'd', 's', 'o', 'n']

things = ["chair", 45, 'A', "house"]

Listing 1.0: Examples of Lists in Python

 

2. Python Tuple

A tuple in python is also a collection of items just like list. Take note of the two key difference:

  • A tuple is immutable (you can’t change the elements once created)
  • A tuple is created with rounded braces

Listing 1.1 show example of Tuple

assets = ("Computer", "Printer", "TV", "Camera")

scores = (56, 45.9, 89.5, 70, 32.9, 67.4)

letters = ('k', 'i', 'n', 'd', 's', 'o', 'n')

things = ("chair", 45, 'A', "house")

Listing 1.1: Example of Tuple in Python

 

3. Python Dictionary

A dictionary in python is a collections of key-value pairs of item. For each entry, there are two items: a key and a value. Note the following about Python dictionaries

  • keys in a dictionary must be unique (no two same keys)
  • keys are immutable
  • keys and values can be of any data types
  • the keys() function returns list of keys in a dictionary
  • the values() function returns list of values in dictionary

Example of dictionaries are given in Listing 1.3.

months = {
    "Jan": "January",
    "Feb": "Febraury",
    "Mar": "March",
    "Apr": "April",
    "May": "May",
    "Jun": "June",
    "Jul": "July",
    "Aug": "August",
    "Sep": "September"
}

weekdays = {
    1: "Monday",
    2: "Tuesday",
    3: "Wednesday",
    4: "Thursday",
    5: "Friday",
    6: "Saturday",
    7: "Sunday"
}

Listing 1.2: Examples of Dictionaries in Python

 

4. Python Set

A set in python is a collection of items just like Lists and Tuples.  Note the following about sets:

  • A set is created using the set keyword
  • A set cannot be an element of a set (but a list can be an element of a list)

Also note: As pointed out by Luka Jeličić Lux, Sets cannot have same element multiple times. Example: When you have set([1,2,2,2,3,3,4,4,5,5]) and print it you get [1,2,3,4,5] as output.

Examples of sets is given in Listing 1.3

assets = set(["Computer", "Printer", "TV", "Camera"])

scores = set([56, 45.9, 89.5, 70, 32.9, 67.4])

letters = set(['k', 'i', 'n', 'd', 's', 'o', 'n'])

things = set(["chair", 45, 'A', "house"])

Listing 1.3: Example of Set in Python

If you prefer the video lesson

In the next lesson, we would examine the operations that can be performed on these data types.















kindsonthegenius

Kindson Munonye is currently completing his doctoral program in Software Engineering in Budapest University of Technology and Economics

View all posts by kindsonthegenius →

4 thoughts on “Difference Between List, Tuple, Set and Dictionary in Python

  1. I have noticed you don’t monetize kindsonthegenius.com,
    don’t waste your traffic, you can earn additional bucks every month with new monetization method.
    This is the best adsense alternative for any type of website (they approve all websites), for more details simply search in gooogle: murgrabia’s tools

  2. I read this post completely regarding the difference of most up-to-date and previous technologies,
    it’s amazing article.

Leave a Reply

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