List vs Tuple vs Set vs Dictionary In Python: 4 Differences (2 Min)

Published: 08 March 2022
on channel: Gokce DB
642
11

In this tutorial, you'll learn 4 differences between List, Tuple, Set & Dictionary in Python.


Facebook:   / gokcedbsql  

Video Transcript:

Connect MySQL In Python:    • How To Connect To MySQL In Python (2 Min) ...  

Hi guys, this is Abhi from Gokcedb. In this video, you're going to learn four differences between list-set tuples and dictionaries in Python. Number one, list tuple set and dictionary are four built-in data types used to store collections in Python.

To create an empty list, use square brackets. For tuples we use parentheses and for dictionary, we use curly braces to leverage the set function to create an empty set. Number two, list and tuple allow duplicates whereas set and dictionary don't allow duplicates.

Watch what happens when I include two key-value pairs with the same key. The first value pair with the duplicate key was ignored. Number three, a list set and dictionary are multiple or changeable whereas a tuple is immutable.

Here I'm using the append function to add a new item to a list and an additional function to add a new item to a set. Use the square brackets to add a new key-value pair to a dictionary. Number four list tuple and dictionary are ordered collections whereas a set is unordered.

Here I'm using the square brackets to print the index element for a list and a tuple. Since a set is unordered it doesn't allow indexing. Watch what happens when I try to print the first indexed element of a set.

I get a type error as expected. There you have it. Make sure you like, subscribe, and turn on the notification bell. Until next time.

1. empty list, tuple, set, dictionary (four built-in data types used to store collections)
my_list = []
my_tuple = ()
my_set = set()
my_dictionary = {}
print('1. list:', my_list, 'tuple:', my_tuple, 'set:', my_set, 'dictionary:', my_dictionary)
# 2. list & tuple allow duplicates, set & dictionary (keys) don't allow duplicates
my_list = [1, 2, 3, 3]
my_tuple = (1, 2, 3, 3)
my_set = {1, 2, 3, 3}
my_dictionary = {1: 'a', 1: 'b', 2: 'c'}
print('2. list:', my_list, 'tuple:', my_tuple, 'set:', my_set, 'dictionary:', my_dictionary)

3. list, set & dictionary are mutable, tuple is immutable which makes them faster
my_list.append(4)
my_set.add(4)
my_dictionary['abc'] = 'd'
print('3. list:', my_list, 'tuple:', my_tuple, 'set:', my_set, 'dictionary:', my_dictionary)

4. list, tuple, dictionary (version 3.7+) are ordered collections, set is unordered (no indexing)
print('4. list first element:', my_list[0], 'tuple first element:', my_tuple[0])
print('4. set first element:', my_set[0])


On this page of the site you can watch the video online List vs Tuple vs Set vs Dictionary In Python: 4 Differences (2 Min) with a duration of hours minute second in good quality, which was uploaded by the user Gokce DB 08 March 2022, share the link with friends and acquaintances, this video has already been watched 642 times on youtube and it was liked by 11 viewers. Enjoy your viewing!