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

preview_player
Показать описание
In this tutorial, you'll learn 4 differences between List, Tuple, Set & Dictionary in Python.



Video Transcript:


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_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])
Рекомендации по теме
Комментарии
Автор

4 differences between List, Tuple, Set & Dictionary in Python are good. Well presented. Good voice. Cool and pleasant explanation. It's a wonderful video program to learn many things. Worth listening and viewing this video.

Mantrimark
join shbcf.ru