List Tuple Set Dictionary
Python එකේ datasets හදන්නෙ මේ containers හතරෙන්. DataFrame column එකක් කියන්නෙ decorated list එකක් වගේ; lookup table එකක් කියන්නෙ dictionary එකක් වගේ.
Datasets in Python are built from these four containers. A DataFrame column is basically a fancy list; a lookup table is basically a dictionary.
Lesson 4 of 64 · Part 1 · Python & Data Basics · Python Fundamentals · bilingual notes (English + සිංහල)
Why this matters. Datasets in Python are built from these four containers. A DataFrame column is basically a fancy list; a lookup table is basically a dictionary.
සිංහලෙන්: Python එකේ datasets හදන්නෙ මේ containers හතරෙන්. DataFrame column එකක් කියන්නෙ decorated list එකක් වගේ; lookup table එකක් කියන්නෙ dictionary එකක් වගේ.
What you'll use this for. Choosing the right container (list vs set vs dict) is what makes your data-cleaning code either fast and clear, or slow and confusing.
සිංහලෙන්: හරි container එක තෝරන එක (list vs set vs dict) තමයි ඔයාගෙ data-cleaning code එක වේගවත් හා පැහැදිලි වෙනවද, නැත්නම් හෙමින් හා ව්යාකූල වෙනවද කියලා තීරණය කරන්නෙ.
Key principles
- Lists keep order, sets remove duplicates. Need to know the 5 unique categories in a column, fast? Turn the list into a set — duplicates vanish instantly.
Lists order එක තියාගන්නවා, sets duplicates අයින් කරනවා - Dictionaries are for lookups, not lists. Mapping a label like "cat"/"dog" to a number for a model is exactly what a dictionary is built for.
Dictionaries කියන්නෙ lookup එකට, list එකට නෙවෙයි
Walkthrough
Types of Objects in Python:
- Immutable Objects
- Mutable Objects
Immutable Objects:
- int
- float
- string
- bool
- tuple
Mutable Objects:
- List
- Set
- Dictionary
List
# list should be included in the square brackets
my_list = [1,2,3,4,5]
print(my_list)
type(my_list)Output
[1, 2, 3, 4, 5]Output
list# lists can have multiple data types
my_list = [2, 3, 1.8, 'English', True]
print(my_list)Output
[2, 3, 1.8, 'English', True]Lists are Mutable --> Changeable
# add elements to a list
my_list = [2, 3, 1.8, 'English', True]
my_list.append(6)
print(my_list)Output
[2, 3, 1.8, 'English', True, 6]# print elements of a list using their index
print(my_list[0])
print(my_list[2])Output
2
1.8# lists allow duplicate values
list_1 = [1,2,3,4,5,12,2,3]
print(list_1)Output
[1, 2, 3, 4, 5, 12, 2, 3]print(len(list_1))Output
8# initiating an empty list
list_2 = []
print(list_2)Output
[]list_2.append(5)
print(list_2)Output
[5]# delete an item in a list
list_2 = [2, 3, 1.8, 'English', True, 6]
print(list_2)
del list_2[2]
print(list_2)Output
[2, 3, 1.8, 'English', True, 6]
[2, 3, 'English', True, 6]# join two lists
list_3 = [1,2,3,4,5]
list_4 = [6,7,8,9,10]
list_5 = list_3 + list_4
print(list_5)Output
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]Tuple
tuple_1 = (2,3,4,5)
print(tuple_1)
type(tuple_1)Output
(2, 3, 4, 5)Output
tuple# tuple allows multiple data types
tuple_2 = (1,2,3.5, 'Machine Learning', False)
print(tuple_2)Output
(1, 2, 3.5, 'Machine Learning', False)# converting a list to a tuple
my_list = [3,4,5,6]
print(my_list)
my_tuple = tuple(my_list)
print(my_tuple)Output
[3, 4, 5, 6]
(3, 4, 5, 6)print(my_tuple[0])
print(my_tuple[1])Output
3
4Tuples are immutable --> Unchangeable
my_tuple.append(6)The notebook raised: AttributeError: 'tuple' object has no attribute 'append'
print(len(my_tuple))Output
4Set
# set --> Curly brackets
my_set = {1,2,3,4,5}
print(my_set)
type(my_set)Output
{1, 2, 3, 4, 5}Output
setprint(my_set[0])The notebook raised: TypeError: 'set' object does not support indexing
# convert a list to a set
list_5 = [4,5,6,7,8]
x = set(list_5)
print(x)Output
{4, 5, 6, 7, 8}# set does not allow duplicate values
set_3 = {1,2,3,4,5,1,2,3}
print(set_3)Output
{1, 2, 3, 4, 5}Dictionary
Key-Value Pair
my_dictionary = {'name':'David','age':30,'country':'India'}
print(my_dictionary)
type(my_dictionary)Output
{'name': 'David', 'age': 30, 'country': 'India'}Output
dictprint(my_dictionary['name'])
print(my_dictionary['age'])
print(my_dictionary['country'])Output
David
30
India# dictionary does not allow duplicate values
dictionary_2 = {'name':'David','age':30,'country':'India','name':'David','age':30,'country':'India'}
print(dictionary_2)Output
{'name': 'David', 'age': 30, 'country': 'India'}Keep going: ← Lesson 3 · Lesson 5 → · Track it in the Self Guide
Finished this one?
Part of Self Guide · Stage 13: ML 1 — Python & Data Basics. Progress is saved in this browser.



