Galle, LK--:--5/6 sites live

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.

2 May 20263 min readSelf Guide · Stage 13#Python & Data Basics#Python Fundamentals#Machine learning

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:

  1. Immutable Objects
  2. Mutable Objects

Immutable Objects:

  1. int
  2. float
  3. string
  4. bool
  5. tuple

Mutable Objects:

  1. List
  2. Set
  3. Dictionary

List

ExamplePython
# list should be included in the square brackets
my_list = [1,2,3,4,5]
print(my_list)
type(my_list)

Output

Output
[1, 2, 3, 4, 5]

Output

Output
list
ExamplePython
# lists can have multiple data types
my_list = [2, 3, 1.8, 'English', True]
print(my_list)

Output

Output
[2, 3, 1.8, 'English', True]

Lists are Mutable --> Changeable

ExamplePython
# add elements to a list
my_list = [2, 3, 1.8, 'English', True]
my_list.append(6)
print(my_list)

Output

Output
[2, 3, 1.8, 'English', True, 6]
ExamplePython
# print elements of a list using their index
print(my_list[0])
print(my_list[2])

Output

Output
2
1.8
ExamplePython
# lists allow duplicate values
list_1 = [1,2,3,4,5,12,2,3]
print(list_1)

Output

Output
[1, 2, 3, 4, 5, 12, 2, 3]
ExamplePython
print(len(list_1))

Output

Output
8
ExamplePython
# initiating an empty list
list_2 = []
print(list_2)

Output

Output
[]
ExamplePython
list_2.append(5)
print(list_2)

Output

Output
[5]
ExamplePython
# 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

Output
[2, 3, 1.8, 'English', True, 6]
[2, 3, 'English', True, 6]
ExamplePython
# 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

Output
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Tuple

ExamplePython
tuple_1 = (2,3,4,5)
print(tuple_1)
type(tuple_1)

Output

Output
(2, 3, 4, 5)

Output

Output
tuple
ExamplePython
# tuple allows multiple data types
tuple_2 = (1,2,3.5, 'Machine Learning', False)
print(tuple_2)

Output

Output
(1, 2, 3.5, 'Machine Learning', False)
ExamplePython
# converting  a list to a tuple

my_list = [3,4,5,6]
print(my_list)

my_tuple = tuple(my_list)
print(my_tuple)

Output

Output
[3, 4, 5, 6]
(3, 4, 5, 6)
ExamplePython
print(my_tuple[0])
print(my_tuple[1])

Output

Output
3
4

Tuples are immutable --> Unchangeable

ExamplePython
my_tuple.append(6)

The notebook raised: AttributeError: 'tuple' object has no attribute 'append'

ExamplePython
print(len(my_tuple))

Output

Output
4

Set

ExamplePython
# set --> Curly brackets
my_set = {1,2,3,4,5}
print(my_set)
type(my_set)

Output

Output
{1, 2, 3, 4, 5}

Output

Output
set
ExamplePython
print(my_set[0])

The notebook raised: TypeError: 'set' object does not support indexing

ExamplePython
# convert a list to a set
list_5 = [4,5,6,7,8]

x = set(list_5)
print(x)

Output

Output
{4, 5, 6, 7, 8}
ExamplePython
# set does not allow duplicate values
set_3 = {1,2,3,4,5,1,2,3}
print(set_3)

Output

Output
{1, 2, 3, 4, 5}

Dictionary

Key-Value Pair

ExamplePython
my_dictionary = {'name':'David','age':30,'country':'India'}
print(my_dictionary)
type(my_dictionary)

Output

Output
{'name': 'David', 'age': 30, 'country': 'India'}

Output

Output
dict
ExamplePython
print(my_dictionary['name'])
print(my_dictionary['age'])
print(my_dictionary['country'])

Output

Output
David
30
India
ExamplePython
# dictionary does not allow duplicate values
dictionary_2 = {'name':'David','age':30,'country':'India','name':'David','age':30,'country':'India'}
print(dictionary_2)

Output

Output
{'name': 'David', 'age': 30, 'country': 'India'}

Keep going: ← Lesson 3 · Lesson 5 → · Track it in the Self Guide