Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

LIST &

DICTIONARY
Luvia Friska Narulita
List

■ List is a data type for collecting data in python


■ The collection of temperature data for all family members can be stored in one place,
one of which can be called a list
■ Beside list, other storage places are dictionaries, tuples, sets etc.
Code Example

#Inisiasi suhu anggota keluarga


Suhu1 = 21
Suhu2 = 20
Suhu3 = 22

#simpan dalam list


Suhu_keluarga = [suhu1, suhu2, suhu3]
Accessing List

■ To access data in lists, python uses something called index.


■ Index shows the position of a data in the list, and python starts the index from 0. Please
note that there are 2 techniques for accessing data in the list. The first is with the
subsetting list, the second is the slicing list.
Example

#Creating list #Subsetting list #Slicing list


height = [162, #index 0 print(height[0]) #first #retrieving data index
position 162 0,1,2,3
177, #index 1
print(height[:4])
182, #index 2
Print(height[-2] #second #retrieving data index
150, #index 3 position from bottom 150 2,3,4
166 #index 4 Print(height[2:5]]
]
List Characteristics

■ List contains a collection of values / data.


■ Lists can contain any data type and the value do not have to all contain the same data
type.
■ List can change
Example

■ Below is the example of #creating list


characteristic no 2
ucupfamily_temp =
■ A list can contain any kind of [‘dad’,19,’ucup’,19,’mom’,20]
data types, such as string,
boyfamily_temp =
numeric, boolean, etc. A list can
[‘father’,20,’boy’,19,’mama’,18]
also contain list

#creating list of list


family_temp = [ucupfamily_temp,
boyfamily_temp]
Manipulating List

■ We can change a list #change the ucup temperature

■ One of the ways to change the list ucupfamily_temp[3] = 22


is by defining index print(ucupfamily_temp)
#change mom into mama_ucup and temp
ucupfamily_temp[-2] = [‘mamah_ucup’,22]
#add list element
ucupfamily_temp = ucupfamily_temp+[‘sister’, 20]
#deleting list element
del(ucupfamily_temp[0])
Some function in List

ucup_family = [‘mom’,’dad’,’ucup’,’sister’] ■ len : return the number of


#len elements in the list. The length of
the list
print(len(ucup_family))
#sorted ■ sorted : return the sorted list
Print(sorted(ucup_family)) ■ sum : sums up the number in the
#sum
list
angka = [12, 7, 9, 10]
Print(sum(angka)
Adding List
Element
methods to add list element: #list
buah = ["jeruk", "apel", "mangga", "duren"]
# add manggis
1. append(item) : adding item from behind
buah.append("manggis")
buah.insert(2,“pepaya")
2. insert(index,item) : adding item from
specific index

append()

color = “Yellow” “Red” “Green”


Beverages Table

Multi “Coffee” “Milk” “Tea”

Dimensional “Apple Juice” “Orange Juice” “Watermelon


Juice”

List “Coffee Ice” “Mix Fruit Ice” “Es Teler”

list_beverages = [
List can also have more than one dimension [“Coffee”, “Milk”, ”Tea”],
[“Apple Juice”, “Orange Juice”, ”Watermelon Juice”],
Multi dimensional list is usually used for [“Coffee Ice”, “Mix Fruit Ice”, “Es Teler”]
]
storing the complex data structure, such as
table, matrix, graph, tree, etc
DICTIONARY
Dictionary?
numbers = {‘one’: 1, ‘two’ : 2, ‘three’: 3}
>>>print(numbers[‘one’])
1

numbers[‘one’] = ‘satu’
>>>print(numbers)

Dictionary is a type of data type in python


that maps the keys and values of a data for k in numbers:
print(“{} = {}”.format(k, numbers[k]))
The main operations on a dictionary are
storing a value with some key and extracting
the value given the key list(numbers.keys())
sorted(numbers.keys())

‘one’ in numbers
‘two’ not in numbers
dict() constructor Looping Techniques

dict([(‘sape’,4139),(‘guido’,4137)]) knights = {‘diponegoro’:’the brave’,


‘kartini’:’the smart’}

dict(sape=4139, guido=4127)
for k,v in knights.items():
print(k,v)
Exercise

■ a = ['1', '13b', 'aa1', 1.32, 22.1, 2.34]
Gunakan slicing list
■ a = [1.32, 22.1, 2.34]
b = ['1', '13b', 'aa1']
c = [3, 40, 100]
Expected output : [ [1.32, 22.1, 2.34], [3, 40, 100], ['1', '13b', 'aa1'] ]
■ a = [
  [5, 9, 8],
    [0, 0, 6]
    ]
Expected Output :
[ [5, 9, 10], [11, 0, 6] ]

You might also like