Lists (အပိုင်း-၁)

Introduction

List သည် data structure အမျိုးအစား တစ်ခု ဖြစ်သည်။ Java, C, C++ စသည့် တခြားသော programming language များတွင် list များကို array များဟု သတ်မှတ်ကြသည်။ Python list သည် တခြားသော programming language တွင်ရှိသည် traditional array ထက်ပို အစွမ်းထက်(powerful)သည်။ (A Python list is more than just a list.)

List ထဲတွင် ပါဝင်သည့် အရာများကို item သို့မဟုတ် element ဟုခေါ်သည်။ List ထဲတွင် ပါဝင်သည့် အရာများကို လေးထောင့်ကွင်း [ ] ထဲတွင် ထည့်ရေးသည်။ Python list ၏ အဓိက အချက်များမှာ

  • List ထဲတွင် ပါဝင်သည့် item သို့မဟုတ် element များ၏ ရှေ့နောက် အစီအစဥ (ordered)သည် အရေးကြီးသည့် အချက်ဖြစ်သည်။
  • List ထဲတွင် arbitrary အမျိုးအစား(type)များ ပါဝင်နိုင်သည်။
  • List ထဲရှိ element များကို index နံပါတ်များသုံး၍ access လုပ်နိုင်သည်။ လှမ်းဖတ်နိုင်သည်။ ကော်ပီကူးနိုင်သည်။
  • List များ ထဲတွင် တခြား list များ ထပ်ခါ ထပ်ခါ ထည့်နိုင်သည်။(arbitrarily nestable)။ List များ ထဲတွင် တခြား list များ sublist အဖြစ်ရှိနေနိုင်သည်။
  • List များ၏ အရွယ်အစား ပြောင်းလဲနိုင်သည်။(variable sizes)
  • List တွင် index နံပါတ်သည် 0 မှ စသည်။
  • List တစ်ခုသည် value များပါဝင်သည့် sequence တစ်ခုဖြစ်သည်။ (A list is a sequence of values.)

List တွင် index နံပါတ်သည် 0 မှ စသည်။ A list is a sequence of values. (List တစ်ခုသည် value များပါဝင်သည့် sequence တစ်ခုဖြစ်သည်။)

Python list ကို အမျိုးအစားမတူသည့် item မျိုးစုံကို ထည့်နိုင်သည့် ပြောင်းနိုင် ပြင်နိုင်သည့် (mutable) container တစ်ခု အဖြစ် နားလည် မှတ်ယူနိုင်သည်။

List ထဲတွင် ရှိနေသည့် item များအားလုံးသည် Python object များ ဖြစ်ကြသည်။

1- List တစ်ခုတည်ဆောက်ခြင်း(Declare a list)

နည်းအမျိုးမျိုးဖြင့် List များကို တည်ဆောက်နိုင်သည်။ အောက်တွင် integer များ ပါသည် list တစ်ခုကို တည်ဆောက်သည်။ လေးထောင့်ကွင်း [ ] ထဲတွင် item များကို comma ခံ၍ ထည့်ရေး၍ list တစ်ခုတည်ဆောက်ခြင်းသည် အလွယ်ကူဆုံးနည်း ဖြစ်သည်။ Element တစ်ခုမှ မပါသည့် list ကို empty list ဟုခေါ်သည်။

In [1]:
# ဘာမှ မပါသည့် Empty list တစ်ခု တည်ဆောက်သည်။
[]
Out[1]:
[]

List ဥပမာများ
[10, 20, 30, 40]
['crunchy frog', 'ram bladder', 'lark vomit']
['spam', 2.0, 5, [10, 20]]

In [2]:
#  နံပါတ်များ(numbers)ပါသည့် list တစ်ခု တည်ဆောက်သည်။(List containing numbers)
L = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] #  လေးထောင့်ကွင်း [ ] ထဲတွင် item များကို comma ခံ၍ ထည့်ရေးသည်။
In [3]:
type(L)  #  L ၏ data type အမျိုးအစား  စစ်သည်။ 
Out[3]:
list
In [4]:
type(L[0])  #  L ထဲတွင် ရှိနေသည့် item သို့မဟုတ် element  ၏ data type အမျိုးအစားကို စစ်သည်။ 
Out[4]:
int
In [5]:
L = list (range(10)) # 0 မှ 9 အထိပါသည် list တစ်ခုကို range() ကို သုံး၍ တည်ဆောက်သည်။ 
L
Out[5]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [6]:
L_str = [ str(c) for c in L] # အပေါ်က list ထဲရှိ integer ကို  string type ပြောင်းသည်။ 
L_str
Out[6]:
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
In [7]:
type(L_str[0])  #  L ထဲတွင် ရှိနေသည့် item ၏ data type အမျိုးအစားကို စစ်သည်။ 
Out[7]:
str

Python သည် dynamic typing ဖြစ်သောကြောင့် အမျိုးအစားမတူသည့် data type များကို list တစ်ထဲတွင် ထည့်ထားနိုင်သည်။ Python data type ထဲမှ မည်သည့် အမျိုးအစား မဆို ဖြစ်နိုင်သည်။
အောက်တွင် bool, str, float, int စသည်တို့ ပါဝင်သည့် Python list တစ်ခုကို ဥပမာ အဖြစ်ဖော်ပြထားသည်။

In [8]:
L3 = [True , '2', 3.0, 4] 
[type (item) for item in L3] #  L3 ထဲတွင် ရှိနေသည့် item ၏ တစ်ခုချင်းစီအမျိုးအစားကို စစ်သည်။ 
Out[8]:
[bool, str, float, int]

1.1 Nested sublists

List တစ်ခုထဲတွင် တခြားသော list သည် sublist အဖြစ် ရှိနေနိုင်သည်။

In [9]:
L4 = ['Bob', 40.0, ['dev', 'mgr']] 
[type (item) for item in L4] #  L4 ထဲတွင် ရှိနေသည့် item ၏ တစ်ခုချင်းစီအမျိုးအစားကို စစ်သည်။ 
Out[9]:
[str, float, list]

List of an iterable’s items, list of successive integers 'Kaung' ဆိုသည့် string တစ်ခု iterable’s item ဖြစ်သောကြောင့် character များပါသည့် list တစ်ခု တည်ဆောက်နိုင်သည်။ String တစ်ခုသည် character အစဥအတန်း တစ်ခုဖြစ်သည်။ (A string is a sequence of characters.) (A string is a sequence of characters.)

In [10]:
L5 = list('Kaung')
L5
Out[10]:
['K', 'a', 'u', 'n', 'g']
In [11]:
[type (item) for item in L5] #  L5 ထဲတွင် ရှိနေသည့် item ၏ တစ်ခုချင်းစီအမျိုးအစားကို စစ်သည်။ 
Out[11]:
[str, str, str, str, str]

1.2 List ၏ index နံပါတ်များ

L[i] # Index
L[i][j] # index of index
L[i:j] #slice
len(L) # length
List ၏ index နံပါတ်များ အလုပ်လုပ်ပုံသည် string ၏ index နံပါတ်များ အလုပ်လုပ်ပုံမှ အတူတူပင် ဖြစ်သည်။ (List indices work the same way as string indices)

  • မည်သည့် integer ကို မဆို အသုံးပြုနိုင်သည်။ (Any integer expression can be used as an index.)
  • List သို့မဟုတ် string ထဲတွင် မရှိသည့် index နံပါတ်ကို ရည်ညွှန်းလျင်၊ ဖတ်လျှင် ၊ ရေးလျှင် IndexError ပေးလိမ့်မည်။( If you try to read or write an element that does not exist, you get an IndexError.)
  • index နံပါတ်တွင် အနှုတ်လက္ခဏာ ပါရှိနေပါက အနောက်မှ စ၍ ပြန်ရေတွက်သည်ဟု မှတ်ပါ။(If an index has a negative value, it counts backward from the end of the list.)

1.3 Mapping

List ကို index နံပါတ်များ နှင့် element များ၏ ဆက်သွယ်ချက် အဖြစ်လည်း မှတ်ယူနိုင်သည်။ (a list as a relationship between indices and elements.)။ ထိုဆက်သွယ်ချက် mapping ဟုခေါ်သည်။ (mapping; each index “maps to” one of the elements.)

In [12]:
#list တွင် in operator ကို သုံးနိုင်သည်။ (The in operator also works on lists.)
cheeses = ['Cheddar', 'Edam', 'Gouda']
print('Edam' in cheeses)
print('Brie' in cheeses)
True
False

1.4 Traversing a List

List အတွင်း element များကို traverse လုပ်ရန် အသုံးများသည့်နည်းများ for loop ဖြစ်သည်။ (The most common way to traverse the elements of a list is with a for loop.)။ string များအတွက်လည်း အတူတူပင် ဖြစ်သည်။

In [13]:
for cheese in cheeses:
 print(cheese)
Cheddar
Edam
Gouda

1.5 List Operations( + operator )

+ operator ဖြင့် list နှစ်ခုကို ဆက်နိုင်သည်။(concatenates lists)

In [14]:
a = [1, 2, 3]
b = [4, 5, 6]
c = a + b
print (c)
[1, 2, 3, 4, 5, 6]
In [15]:
d =  str([1, 2]) + "34" # Same as "[1, 2]" + "34"
print(d)

e =  [1, 2] + list("34") # Same as [1, 2] + ["3", "4"]
print(e)
[1, 2]34
[1, 2, '3', '4']

1.6 Lists and Strings

String သည် အက္ခရာစဥ◌်အတန်း(sequence of characters )ဖြစ်သည်။ list သည် value စဥ◌်အတန်း(sequence of values)ဖြစ်သည်။ (A string is a sequence of characters and a list is a sequence of values.)

In [16]:
s = 'spam'
t = list(s)
print (t)
['s', 'p', 'a', 'm']
In [17]:
# string တစ်ခုကို word အဖြစ် ပိုင်းခြားလိုလျှင် split method ကို အသုံးပြုသည်။ 
s = 'pining for the fjords'
t = s.split()
print (t)
['pining', 'for', 'the', 'fjords']
In [18]:
# hyphen ကို delimiter အဖြစ်သုံး၍ ပိုင်းခြားသည်။
s = 'spam-spam-spam'
delimiter = '-' # uses a hyphen as a delimiter:
s.split(delimiter)
Out[18]:
['spam', 'spam', 'spam']
In [19]:
# split လုပ်ထားသည့် item များကို ပြန်ပေါင်းစပ်(Join)သည်။ Join is a string method.
t = ['pining', 'for', 'the', 'fjords']
delimiter = ' ' #nvoke it on the delimiter and pass the list as a pa rameter:
delimiter.join(t)
Out[19]:
'pining for the fjords'

1.7 List Operations( * operator )

In [20]:
#  * operator  ဖြင့်  list များကို အဆများစွာ ပွားနိုင်သည်။(repeats a list a given number of times) 
f = ['Ni!'] * 4 # repeats ['Ni!'] four times
print(d)

g = [1, 2, 3] * 3 #repeats the list [1, 2, 3] three times.
print(e)
[1, 2]34
[1, 2, '3', '4']

1.8 List Iteration and Comprehensions

For loop သုံး၍ iteration လုပ်သည်။

In [21]:
for x in [1, 2, 3]:
    print(x, end=' ') # Iteration (end=' '  ကြောင့် item များ အလျားလိုက်ပေါ်သည်။ )
1 2 3 
In [22]:
res = [c * 4 for c in 'SPAM'] # List comprehensions
res
Out[22]:
['SSSS', 'PPPP', 'AAAA', 'MMMM']
In [23]:
#List ထဲမှာ item တစ်ခုချင်းစီကို ၄ခု ဖြစ်အောင် မြှောက်ပြီးမှ .append() လုပ်၍ empty list ထဲသို့ ထည့်သည်။ 
res = []
for c in 'SPAM': # List comprehension equivalent
    res.append(c * 4)

res
Out[23]:
['SSSS', 'PPPP', 'AAAA', 'MMMM']

List Slices The slice operator also works on lists:

In [24]:
t = ['a', 'b', 'c', 'd', 'e', 'f']
t[1:3]
Out[24]:
['b', 'c']

စမည့် index နံပါတ်ကို မထည့်ပေးလျှင် 0 မှ စရမည်။ ဥပမာ - t[:4] ဆုံးမည့် index နံပါတ်ကို မထည့်ပေးလျှင် နောက်ဆုံးအထိယူရမည်။ ဥပမာ - t[3:] စမည့် index နံပါတ် နှင့် ဆုံးမည့် index နံပါတ်ကို မထည့်ပေးလျှင် အစမှ အဆုံးအထိ ဖြစ်သည်။

In [25]:
t[:4] # စမည့် index နံပါတ်ကို မထည့်ပေးလျှင် 0 မှ စရမည်။
Out[25]:
['a', 'b', 'c', 'd']
In [26]:
t[3:] #  ဆုံးမည့် index နံပါတ်ကို မထည့်ပေးလျှင် နောက်ဆုံးအထိယူရမည်။ 
Out[26]:
['d', 'e', 'f']
In [27]:
t[:] #  စမည့် index နံပါတ် နှင့် ဆုံးမည့် index နံပါတ်ကို မထည့်ပေးလျှင် အစမှ အဆုံးအထိ ဖြစ်သည်။ 
Out[27]:
['a', 'b', 'c', 'd', 'e', 'f']

2- List တစ်ခု အတွင်း သို့ item များထည့်ခြင်း (Add Items in a list)

2.1 List ၏ နောက်ဆုံးနေရာ၌ item များထည့်ခြင်း(add item at the end of a list)

Itemတစ်ခု(single item)ကို list ၏ နောက်ဆုံးနေရာတွင် ထည့်သွင်းလိုသည့်အခါ .append() ကို သုံးသည်။

In [28]:
programming_languages = ["Python","Java","R","Ruby and Rails"] 
In [29]:
programming_languages.append("C++") 
programming_languages
Out[29]:
['Python', 'Java', 'R', 'Ruby and Rails', 'C++']

2.2 List ၏ သတ်မှတ်ထားသည့်နေရာတွင် (particular place) item များထည့်ခြင်း(add item at particular place of a list / insert items in a list)

List ၏ သတ်မှတ်ထားသောနေရာတွင် item ကို ထည့်ပေး(insert လုပ်ပေး)သည်။ ထည့်ပေးစေလိုသည့်နေရာ၏ index နံပါတ် ထည့်ပေးရသည်။

In [30]:
programming_languages.insert(2,"C#")  #ထည့်ပေးစေလိုသည့်နေရာ၏ index နံပါတ်  = 2
programming_languages
Out[30]:
['Python', 'Java', 'C#', 'R', 'Ruby and Rails', 'C++']

3- List ထဲမှာ item များကို ဖျက်ခြင်း(Delete items from the list)

3.1 List ထဲမှာနောက်ဆုံး item ကို ဖျက်ခြင်း(delete item at the end of a list)

ထည့်ပေးလိုက်သည့် index နံပါတ်နှင့် ကိုက်ညီသည့် item ကိုဖယ်ထုတ်ပေးသည်။ အကယ်၍ ဖယ်ထုတ်ရမည့် item နံပါတ်ကို မထည့်ပေးပါက list ထဲမှာ နောက်ဆုံး(last) item ကို ဖယ်ထုတ်ပေးသည်။

In [31]:
programming_languages.pop()
Out[31]:
'C++'
In [32]:
programming_languages
Out[32]:
['Python', 'Java', 'C#', 'R', 'Ruby and Rails']
In [33]:
programming_languages.pop(3)
Out[33]:
'R'
In [34]:
programming_languages
Out[34]:
['Python', 'Java', 'C#', 'Ruby and Rails']

3.2 List ၏ သတ်မှတ်ထားသည့်နေရာ item အား ဖျက်ခြင်း(delete item from particular position of a list)

ထည့်ပေးလိုက်သည့် index နံပါတ်နှင့် ကိုက်ညီသည့် item ကို ဖျက်ပေးသည်။

In [35]:
del(programming_languages[2])
In [36]:
programming_languages
Out[36]:
['Python', 'Java', 'Ruby and Rails']

3.3 List ၏ သတ်မှတ်ထားသည့် value အား ဖျက်ခြင်း (delete item of particular value in a list)

ထည့်ပေးလိုက်သည့် value နှင့် ကိုက်ညီသည့် item ကို ဖျက်ပေးသည်။ ထည့်ပေးလိုက်သည့် value နှင့် ကိုက်ညီသည့် item မရှိပါက ValueError ပေးသည်။

In [37]:
programming_languages.remove("Ruby and Rails") 
In [38]:
programming_languages
Out[38]:
['Python', 'Java']

Deleting Elements (.pop() , del , .remove())

In [39]:
t = ['a', 'b', 'c']
x = t.pop(1)  #.pop()
print(t) 
print(x)
['a', 'c']
b
In [40]:
t = ['a', 'b', 'c']
del t[1]   # del
print(t)
['a', 'c']
In [41]:
t = ['a', 'b', 'c']
t.remove('b')
print(t)
['a', 'c']
In [42]:
t = ['a', 'b', 'c', 'd', 'e', 'f']
del t[1:5] # Index နံပါတ် (၁)မှ (၅) မတိုင်ခင် အထိဖျက်မည်။ ( Index နံပါတ် (၅) မပါ။)
print(t)
['a', 'f']

4- List အတွင်း ရှိ item များအား Update လုပ်ခြင်း (Update item)

Index နံပါတ်ပေး၍ list ထဲမှာ item များကို update လုပ်နိုင်သည်။

In [43]:
programming_languages[1] = 'C++'
In [44]:
programming_languages
Out[44]:
['Python', 'C++']

5- Get a particular item

Index နံပါတ် ဖြင့် access လုပ်နိုင်သည်။

In [45]:
programming_languages[1]  # (i) from start
Out[45]:
'C++'
In [46]:
programming_languages[-1]  # (ii) from end)
Out[46]:
'C++'

List ၏ နောက်ဆုံးနေရာမှ ထပ်ထည့်လိုသည့်အခါ .append() ကို သုံးသည်။

In [47]:
programming_languages.append("Java")
programming_languages.append("R")
programming_languages.append("Ruby and Rails")
In [48]:
programming_languages
Out[48]:
['Python', 'C++', 'Java', 'R', 'Ruby and Rails']

6- ရှေ့နောက် အစီအစဥ ပြောင်းပြန်လှန်ခြင်း (Reverse a list)

In [49]:
programming_languages.reverse() 
In [50]:
programming_languages
Out[50]:
['Ruby and Rails', 'R', 'Java', 'C++', 'Python']

7- Finding no.of items in a list/ Length of a list

In [51]:
len(programming_languages)
Out[51]:
5

8- ရှေ့နောက်စဥ◌် ခြင်း (Sorting a list)

8.1 Permenantly sort in ascending order

Integers list ဖြစ်လျှင် ကိန်းတန််ဖိုးအတိုင်း စီသည်။ String list ဖြစ်လျှင် အက္ခရာစဥ◌်တိုင်း စီသည်။

In [52]:
numbers = [1, 3, 4, 2] 
numbers.sort() # Sorting list of Integers in ascending 
print(numbers) 
[1, 2, 3, 4]
In [53]:
strs = ["geeks", "code", "ide", "practice"] 
strs.sort() # Sorting list of Integers in ascending 
print(strs) 
['code', 'geeks', 'ide', 'practice']
In [54]:
numbers = [1, 3, 4, 2] 
numbers.sort(reverse = True) # Sorting list of Integers in descending 
print(numbers) 
[4, 3, 2, 1]

8.2 Permenantly sort in descending order

In [55]:
programming_languages.sort(reverse = True)
In [56]:
programming_languages
Out[56]:
['Ruby and Rails', 'R', 'Python', 'Java', 'C++']
In [57]:
programming_languages = ['Ruby and Rails', 'R', 'Java', 'C++', 'Python']

8.3 Temporary sort ( sorted() )

In [58]:
sorted(programming_languages)
Out[58]:
['C++', 'Java', 'Python', 'R', 'Ruby and Rails']
In [59]:
programming_languages
Out[59]:
['Ruby and Rails', 'R', 'Java', 'C++', 'Python']

9- Accessing list items one by one through looping

In [60]:
for language in programming_languages:
    print(language)
Ruby and Rails
R
Java
C++
Python
In [61]:
programming_languages[1:4] # (i) Access items 1-3
Out[61]:
['R', 'Java', 'C++']
In [62]:
programming_languages[1:] # (ii) Access items 1-end
Out[62]:
['R', 'Java', 'C++', 'Python']
In [63]:
programming_languages[:4] # (iii) Access items start-3
Out[63]:
['Ruby and Rails', 'R', 'Java', 'C++']
In [64]:
programming_languages[-1:] # (iv) Access item at the end
Out[64]:
['Python']
In [65]:
programming_languages[:-4] # (v) start from end and Access 4th item
Out[65]:
['Ruby and Rails']
In [66]:
programming_languages[:] # (vi) Access all items
Out[66]:
['Ruby and Rails', 'R', 'Java', 'C++', 'Python']
In [67]:
programming_languages
Out[67]:
['Ruby and Rails', 'R', 'Java', 'C++', 'Python']

10- ကော်ပီကူးခြင်း (Copy a list)

10.1 List တစ်ခုလုံးကို ကော်ပီကူးခြင်း (Copy whole list)

In [68]:
new_programming_languages = programming_languages[:] 
In [69]:
new_programming_languages
Out[69]:
['Ruby and Rails', 'R', 'Java', 'C++', 'Python']
In [70]:
programming_languages
Out[70]:
['Ruby and Rails', 'R', 'Java', 'C++', 'Python']

10.2 Item များ တစ်ခုချင်းစီကို ကော်ပီကူခြင်း(Copy list items one by one)

In [71]:
new_programming_languages = [] 
i = 0
for language in programming_languages:
    new_programming_languages.append(programming_languages.pop(i))
    i += 1
    i
In [72]:
programming_languages
Out[72]:
['R', 'C++']
In [73]:
new_programming_languages
Out[73]:
['Ruby and Rails', 'Java', 'Python']
In [74]:
programming_languages = ['Ruby and Rails', 'R', 'Java', 'C++', 'Python']
In [75]:
programming_languages
Out[75]:
['Ruby and Rails', 'R', 'Java', 'C++', 'Python']

11- if statement with a list

11.1 List ထဲတွင် မိမိသိလိုသည့် value တစ်ခု ရှိ မရှိ စစ်ဆေးခြင်း (Checking whether a value is in a list)

In [76]:
if 'R' in programming_languages:
    print('exist')
exist

11.2 List ထဲတွင် မိမိသိလိုသည့် value တစ်ခု မရှိခြင်းကို စစ်ဆေးခြင်း (Checking whether a value not in a list.)

In [77]:
if 'R' not in programming_languages:
    print('not exist')     
else:
    print('exist')
exist

11.3 List ထဲတွင် ဘာမှ ရှိမရှိ စစ်ဆေးခြင်း(Checking whether list is empty or not)

In [78]:
programming_languages = [ ] 
if programming_languages:
    print('There exist item or items')
else:
    print('List is empty')
List is empty
OR
In [79]:
programming_languages = [ ] 
if not programming_languages:
    print('List is empty')
else:
    print('There exist item or items')
List is empty

12- For loop နှင့် pop item ကို သုံး၍ List ထဲမှ item များ ကို တစ်ခုပြီးတစ်ခု ဖယ်ထုတ်ခြင်း(pop item one by one from one list to another (using for loop))

In [80]:
programming_languages = ['Ruby and Rails', 'R', 'Java', 'C++', 'Python'] 
new_programming_languages = []
for i in range(0,len(programming_languages)):
    new_programming_languages.append(programming_languages.pop())
In [81]:
programming_languages
Out[81]:
[]
In [82]:
new_programming_languages
Out[82]:
['Python', 'C++', 'Java', 'R', 'Ruby and Rails']

13- While loop , append item နှင့် pop item ကို သုံး၍ List တစ်ခုထဲမှ item များကို ဖယ်ထုတ်ပြီး တခြား List ထဲသို့ ထည့်ခြင်း(pop item one by one from one list to another (using while loop))

In [83]:
while new_programming_languages: 
    programming_languages.append(new_programming_languages.pop())
In [84]:
new_programming_languages
Out[84]:
[]
In [85]:
programming_languages
Out[85]:
['Ruby and Rails', 'R', 'Java', 'C++', 'Python']

14- Value ဖြင့် list ထဲမှ instance များကို ဖယ်ထုတ်ခြင်း(Removing all instances of particular value from a list)

if ကို သုံး၍ ပထမဆုံး instance ကို အရင် ဖယ်ထုတ်သည်။

In [86]:
# The logic below only remove 1st instance of particular value
programming_languages = ['Ruby and Rails', 'R', 'Java', 'C++', 'Python','C++','R','C++'] 
if 'C++' in programming_languages:
    programming_languages.remove('C++')
In [87]:
programming_languages
Out[87]:
['Ruby and Rails', 'R', 'Java', 'Python', 'C++', 'R', 'C++']
In [88]:
# For loop ကို သုံး၍  ရှိသမျှ instance အားလုံးကို ဖယ်ထုတ်သည်။ 
# ဖယ်ထုတ်ရမည့် instance မကုန်မခြင်း For loop ရပ်လိမ့်မည် မဟုတ်ပေ။

programming_languages = ['Ruby and Rails', 'R', 'Java', 'C++', 'Python','C++','R','C++'] 
while 'C++' in programming_languages:
    programming_languages.remove('C++')
In [89]:
programming_languages
Out[89]:
['Ruby and Rails', 'R', 'Java', 'Python', 'R']

15 Set operation သုံးခြင်း (Applying set operation on a list)

List ကို set() ဖြင့် set အဖြစ်သို့ ပြောင်းသည်။

In [90]:
# If a value repeated in a list & we want only that value occur only once'''
programming_languages = ['Ruby and Rails', 'R', 'Java', 'Python', 'C++', 'R', 'C++']
print(set(programming_languages)) 
{'Ruby and Rails', 'R', 'C++', 'Java', 'Python'}
In [91]:
type(set(programming_languages)) # Set   ဖြစ် မဖြစ် စစ်သည်။ 
Out[91]:
set
In [92]:
programming_languages = ['Ruby and Rails', 'R', 'Java', 'Python', 'C++', 'R', 'C++']
set_programming_languages = set(programming_languages)
print(set_programming_languages)
{'Ruby and Rails', 'R', 'C++', 'Java', 'Python'}

16- List ကို tuple အဖြစ်သို့ ပြောင်းခြင်း(Convert list into a tuple)

In [93]:
programming_languages = ['Ruby and Rails', 'R', 'Java', 'Python', 'C++', 'R', 'C++']
tuple_programming_languages = tuple(programming_languages)
print(tuple_programming_languages)
('Ruby and Rails', 'R', 'Java', 'Python', 'C++', 'R', 'C++')
In [94]:
type(tuple_programming_languages) #  tuple  ဖြစ် မဖြစ် စစ်သည်။ 
Out[94]:
tuple

17- List နှင့် nested for loop ကို တွဲသုံးခြင်း (Use nested for loop with a list)

Empty list တစ်ခု ပြုလုပ်သည်။ first_names နှင့် last_names list ထဲမှာ နာမည်များကို nested for loop ဖြင့် တွဲ၍ ထို empty list ထဲသို့ ဖြင့် .append() ထည့်သည်။

In [95]:
first_names = ['Maung',' U','Ko'] 
last_names = ['Ba Kaung',' Myint Soe','Sai',' Hla ','Htoo']
name = []
for f_name in first_names:
    for l_name in last_names:
        name.append(f_name+" "+l_name)
name
Out[95]:
['Maung Ba Kaung',
 'Maung  Myint Soe',
 'Maung Sai',
 'Maung  Hla ',
 'Maung Htoo',
 ' U Ba Kaung',
 ' U  Myint Soe',
 ' U Sai',
 ' U  Hla ',
 ' U Htoo',
 'Ko Ba Kaung',
 'Ko  Myint Soe',
 'Ko Sai',
 'Ko  Hla ',
 'Ko Htoo']

End of Lists (အပိုင်း-၁)

In [ ]: