KEMBAR78
Python Programming Essentials - M12 - Lists | PPTX
http://www.skillbrew.com
/SkillbrewTalent brewed by the
industry itself
Python Lists and references
Pavan Verma
@YinYangPavan
Python Programming Essentials
© SkillBrew http://skillbrew.com
Python lists
 List is an ordered collections of items
 Item could be any Python data type (strings, number, other
lists, tuple, dictionary)
 Similar to arrays in (C / JAVA / C++)
 Lists are the simplest data structure in Python
colors = [‘red’, ‘blue’, ‘green’]
2
© SkillBrew http://skillbrew.com
List creation
Use square brackets [] to create a list
3
>>> colors = ['red', 'blue', 'green']
>>> colors
Output:
['red', 'blue', 'green']
© SkillBrew http://skillbrew.com
Access List Elements
list[index]
4
>>> colors = ['red', 'blue', 'green']
>>> colors
['red', 'blue', 'green']
>>> colors[0]
'red'
>>> colors[1]
'blue'
>>> colors[2]
'green'
© SkillBrew http://skillbrew.com
Negative Indexes
list[-index]
5
>>> colors = ['red', 'blue', 'green']
>>> colors
['red', 'blue', 'green']
>>> colors[-1]
'green'
>>> colors[-2]
'blue'
>>> colors[-3]
'red'
© SkillBrew http://skillbrew.com
List operations
 length
 append
 insert
 remove
 delete
 pop
 Slicing
6
© SkillBrew http://skillbrew.com
Length of a list
Use len()function to get the length of list
7
>>> colors = ['red', 'blue', 'green']
>>> len(colors)
Output:
3
© SkillBrew http://skillbrew.com
Append an element
 Use append() to add an element to the list
 append adds the element at the end of a list
8
>>> colors = ['red', 'blue', 'green']
>>> colors.append('orange')
>>> colors
['red', 'blue', 'green', 'orange']
© SkillBrew http://skillbrew.com
Insert an element at an index
 list.insert(index, value)
 Use insert to insert an element at a particular index
9
>>> colors ['red', 'blue', 'green', 'orange']
>>> colors.insert(1, 'black')
>>> colors
['red', 'black', 'blue', 'green', 'orange']
© SkillBrew http://skillbrew.com
Remove an element from list
remove(x) removes the first item from the list whose
value is x
10
>>> colors
['red', 'black', 'blue', 'green', 'orange']
>>> colors.remove('black')
>>> colors
['red', 'blue', 'green', 'orange']
© SkillBrew http://skillbrew.com
Remove an element from list (2)
If you try to remove an element which is not there in the list a
ValueError is raised
11
>>> colors
['red', 'blue', 'green', 'orange']
>>> colors.remove('yellow')
Traceback (most recent call last): File
"<pyshell#24>", line 1, in <module>
colors.remove('yellow')
ValueError: list.remove(x): x not in list
© SkillBrew http://skillbrew.com
Delete an element from list
del list[index]
Use del operator to delete an element from a list
12
>>> colors
['red', 'blue', 'green', 'orange']
>>> del colors[1]
>>> colors
['red', 'green', 'orange']
© SkillBrew http://skillbrew.com
Sorting a list
 sort()sorts the list in place
 In place means that sort works on the original
list rather than giving back a new list
13
>>> colors
['red', 'green', 'orange']
>>> colors.sort()
>>> colors
['green', 'orange', 'red']
© SkillBrew http://skillbrew.com
Reverse a list
 reverse()reverses the list in place
 In place means that reverse()works on the
original list rather than giving back a new list
14
>>> colors
['green', 'orange', 'red']
>>> colors.reverse()
>>> colors
['red', 'orange', 'green']
© SkillBrew http://skillbrew.com
Using list as stack
 A stack is a list with just two operations:
• Push
• Pop
15
© SkillBrew http://skillbrew.com
Stack – push
40
30
20
10
16
50
40
30
20
10
stack.append(50)
>>> stack = [10, 20, 30, 40]
>>> stack.append(50)
>>> stack
[10, 20, 30, 40, 50]
© SkillBrew http://skillbrew.com
Stack – pop
17
50
40
30
20
10
stack.pop( )
>>> stack
[10, 20, 30, 40, 50]
>>> stack.pop()
50
>>> stack
[10, 20, 30, 40]
40
30
20
10
© SkillBrew http://skillbrew.com
List slicing
 Slicing: Extracting parts of list
 Syntax:
list[start:end]
list[start:]
list[end:]
list[:]
 start inclusive and excluding end
 Slicing returns a new list
18
© SkillBrew http://skillbrew.com
List slicing (2)
19
>>> colors = ['yellow', 'red', 'blue', 'green', 'black']
>>> colors[0:]
['yellow', 'red', 'blue', 'green', 'black']
>>> colors[:4]
['yellow', 'red', 'blue', 'green']
>>> colors[1:3]
['red', 'blue']
>>> colors[:]
['yellow', 'red', 'blue', 'green', 'black']
© SkillBrew http://skillbrew.com
Summary
 What is a list
 List creation
 Access elements of a list
 Basic List operations
 Sorting and reversing a list
 Using list as stack
 Slicing
20
© SkillBrew http://skillbrew.com
Resources
 Tutorial on lists
http://www.tutorialspoint.com/Python/Python_lists.htm
21
22

Python Programming Essentials - M12 - Lists

  • 1.
    http://www.skillbrew.com /SkillbrewTalent brewed bythe industry itself Python Lists and references Pavan Verma @YinYangPavan Python Programming Essentials
  • 2.
    © SkillBrew http://skillbrew.com Pythonlists  List is an ordered collections of items  Item could be any Python data type (strings, number, other lists, tuple, dictionary)  Similar to arrays in (C / JAVA / C++)  Lists are the simplest data structure in Python colors = [‘red’, ‘blue’, ‘green’] 2
  • 3.
    © SkillBrew http://skillbrew.com Listcreation Use square brackets [] to create a list 3 >>> colors = ['red', 'blue', 'green'] >>> colors Output: ['red', 'blue', 'green']
  • 4.
    © SkillBrew http://skillbrew.com AccessList Elements list[index] 4 >>> colors = ['red', 'blue', 'green'] >>> colors ['red', 'blue', 'green'] >>> colors[0] 'red' >>> colors[1] 'blue' >>> colors[2] 'green'
  • 5.
    © SkillBrew http://skillbrew.com NegativeIndexes list[-index] 5 >>> colors = ['red', 'blue', 'green'] >>> colors ['red', 'blue', 'green'] >>> colors[-1] 'green' >>> colors[-2] 'blue' >>> colors[-3] 'red'
  • 6.
    © SkillBrew http://skillbrew.com Listoperations  length  append  insert  remove  delete  pop  Slicing 6
  • 7.
    © SkillBrew http://skillbrew.com Lengthof a list Use len()function to get the length of list 7 >>> colors = ['red', 'blue', 'green'] >>> len(colors) Output: 3
  • 8.
    © SkillBrew http://skillbrew.com Appendan element  Use append() to add an element to the list  append adds the element at the end of a list 8 >>> colors = ['red', 'blue', 'green'] >>> colors.append('orange') >>> colors ['red', 'blue', 'green', 'orange']
  • 9.
    © SkillBrew http://skillbrew.com Insertan element at an index  list.insert(index, value)  Use insert to insert an element at a particular index 9 >>> colors ['red', 'blue', 'green', 'orange'] >>> colors.insert(1, 'black') >>> colors ['red', 'black', 'blue', 'green', 'orange']
  • 10.
    © SkillBrew http://skillbrew.com Removean element from list remove(x) removes the first item from the list whose value is x 10 >>> colors ['red', 'black', 'blue', 'green', 'orange'] >>> colors.remove('black') >>> colors ['red', 'blue', 'green', 'orange']
  • 11.
    © SkillBrew http://skillbrew.com Removean element from list (2) If you try to remove an element which is not there in the list a ValueError is raised 11 >>> colors ['red', 'blue', 'green', 'orange'] >>> colors.remove('yellow') Traceback (most recent call last): File "<pyshell#24>", line 1, in <module> colors.remove('yellow') ValueError: list.remove(x): x not in list
  • 12.
    © SkillBrew http://skillbrew.com Deletean element from list del list[index] Use del operator to delete an element from a list 12 >>> colors ['red', 'blue', 'green', 'orange'] >>> del colors[1] >>> colors ['red', 'green', 'orange']
  • 13.
    © SkillBrew http://skillbrew.com Sortinga list  sort()sorts the list in place  In place means that sort works on the original list rather than giving back a new list 13 >>> colors ['red', 'green', 'orange'] >>> colors.sort() >>> colors ['green', 'orange', 'red']
  • 14.
    © SkillBrew http://skillbrew.com Reversea list  reverse()reverses the list in place  In place means that reverse()works on the original list rather than giving back a new list 14 >>> colors ['green', 'orange', 'red'] >>> colors.reverse() >>> colors ['red', 'orange', 'green']
  • 15.
    © SkillBrew http://skillbrew.com Usinglist as stack  A stack is a list with just two operations: • Push • Pop 15
  • 16.
    © SkillBrew http://skillbrew.com Stack– push 40 30 20 10 16 50 40 30 20 10 stack.append(50) >>> stack = [10, 20, 30, 40] >>> stack.append(50) >>> stack [10, 20, 30, 40, 50]
  • 17.
    © SkillBrew http://skillbrew.com Stack– pop 17 50 40 30 20 10 stack.pop( ) >>> stack [10, 20, 30, 40, 50] >>> stack.pop() 50 >>> stack [10, 20, 30, 40] 40 30 20 10
  • 18.
    © SkillBrew http://skillbrew.com Listslicing  Slicing: Extracting parts of list  Syntax: list[start:end] list[start:] list[end:] list[:]  start inclusive and excluding end  Slicing returns a new list 18
  • 19.
    © SkillBrew http://skillbrew.com Listslicing (2) 19 >>> colors = ['yellow', 'red', 'blue', 'green', 'black'] >>> colors[0:] ['yellow', 'red', 'blue', 'green', 'black'] >>> colors[:4] ['yellow', 'red', 'blue', 'green'] >>> colors[1:3] ['red', 'blue'] >>> colors[:] ['yellow', 'red', 'blue', 'green', 'black']
  • 20.
    © SkillBrew http://skillbrew.com Summary What is a list  List creation  Access elements of a list  Basic List operations  Sorting and reversing a list  Using list as stack  Slicing 20
  • 21.
    © SkillBrew http://skillbrew.com Resources Tutorial on lists http://www.tutorialspoint.com/Python/Python_lists.htm 21
  • 22.

Editor's Notes

  • #16 Here the intention is to implement a stack using lists
  • #17 Here we have a stack with 10, 20, 30, 40 already present append is equivalent to a PUSH operation in a stack
  • #18 Stack after append operation has 50 on top pop will remove 50 from top, just like POP in a stack Data Structure
  • #19 These have been covered in detail in strings hence just a brief overview of slicing is presented here