SlideShare a Scribd company logo
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

More Related Content

PPTX
LIST IN PYTHON
PDF
Python unit 2 as per Anna university syllabus
PPTX
Bucket sort- A Noncomparision Algorithm
PPTX
PDF
Trees (slides)
PDF
Introduction to Python
PPTX
data frames.pptx
PDF
Object oriented approach in python programming
LIST IN PYTHON
Python unit 2 as per Anna university syllabus
Bucket sort- A Noncomparision Algorithm
Trees (slides)
Introduction to Python
data frames.pptx
Object oriented approach in python programming

What's hot (20)

PPTX
Unit 5 java-awt (1)
PPTX
Boxing & unboxing
PPTX
String function in my sql
PPT
Line drawing algorithm and antialiasing techniques
PDF
Hashing and Hash Tables
PPTX
Interpreter
PDF
Code generation in Compiler Design
PPTX
Queue in Data Structure
PDF
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
PPTX
POLYNOMIAL ADDITION USING LINKED LIST.pptx
PPTX
Basic of array and data structure, data structure basics, array, address calc...
DOCX
Lab manual asp.net
PPTX
Member Function in C++
PPT
Operator Overloading
PPTX
Array in c language
PPT
Fundamental of C Programming Language and Basic Input/Output Function
PPTX
Java constructors
PPT
PPTX
PPTX
Top down parsing
Unit 5 java-awt (1)
Boxing & unboxing
String function in my sql
Line drawing algorithm and antialiasing techniques
Hashing and Hash Tables
Interpreter
Code generation in Compiler Design
Queue in Data Structure
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
POLYNOMIAL ADDITION USING LINKED LIST.pptx
Basic of array and data structure, data structure basics, array, address calc...
Lab manual asp.net
Member Function in C++
Operator Overloading
Array in c language
Fundamental of C Programming Language and Basic Input/Output Function
Java constructors
Top down parsing
Ad

Viewers also liked (8)

PPTX
Class 7: Programming with Lists
PPTX
PPTX
An Introduction To Python - Dictionaries
PDF
Python Workshop Part 2. LUG Maniapl
PDF
Python Programming and GIS
PDF
Python Worst Practices
PPT
Introduction to Python
Class 7: Programming with Lists
An Introduction To Python - Dictionaries
Python Workshop Part 2. LUG Maniapl
Python Programming and GIS
Python Worst Practices
Introduction to Python
Ad

Similar to Python Programming Essentials - M12 - Lists (20)

PPT
UNIT 4 PY.ppt - DATA STRUCTURE IN PYTHON
PPTX
Pythonlearn-08-Lists.pptx
PPTX
Python list tuple dictionary .pptx
PPTX
R Data Structure.pptx
PPTX
Pa1 session 3_slides
PPTX
Pythonlearn-08-Lists (1).pptxaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
PDF
Python Programming: Lists, Modules, Exceptions
PPTX
Python Lecture 8
PDF
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
PPTX
Python programming workshop session 3
PPT
UNIT III_Python Programming_aditya COllege
PPT
UNIT III_Python Programming_aditya COllege
PPTX
Pythonlearn-08-Lists.pptx
PPTX
list and control statement.pptx
PPTX
Lists.pptx
PPTX
Array functions for all languages prog.pptx
PPTX
Array functions using php programming language.pptx
PDF
PureScript & Pux
UNIT 4 PY.ppt - DATA STRUCTURE IN PYTHON
Pythonlearn-08-Lists.pptx
Python list tuple dictionary .pptx
R Data Structure.pptx
Pa1 session 3_slides
Pythonlearn-08-Lists (1).pptxaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
Python Programming: Lists, Modules, Exceptions
Python Lecture 8
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
Python programming workshop session 3
UNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllege
Pythonlearn-08-Lists.pptx
list and control statement.pptx
Lists.pptx
Array functions for all languages prog.pptx
Array functions using php programming language.pptx
PureScript & Pux

More from P3 InfoTech Solutions Pvt. Ltd. (20)

PPTX
Python Programming Essentials - M44 - Overview of Web Development
PPTX
Python Programming Essentials - M40 - Invoking External Programs
PPTX
Python Programming Essentials - M39 - Unit Testing
PPTX
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
PPTX
Python Programming Essentials - M35 - Iterators & Generators
PPTX
Python Programming Essentials - M34 - List Comprehensions
PPTX
Python Programming Essentials - M31 - PEP 8
PPTX
Python Programming Essentials - M29 - Python Interpreter and Files
PPTX
Python Programming Essentials - M28 - Debugging with pdb
PPTX
Python Programming Essentials - M27 - Logging module
PPTX
Python Programming Essentials - M25 - os and sys modules
PPTX
Python Programming Essentials - M24 - math module
PPTX
Python Programming Essentials - M23 - datetime module
PPTX
Python Programming Essentials - M22 - File Operations
PPTX
Python Programming Essentials - M21 - Exception Handling
PPTX
Python Programming Essentials - M20 - Classes and Objects
PPTX
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
PPTX
Python Programming Essentials - M18 - Modules and Packages
PPTX
Python Programming Essentials - M17 - Functions
PPTX
Python Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M44 - Overview of Web Development
Python Programming Essentials - M40 - Invoking External Programs
Python Programming Essentials - M39 - Unit Testing
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Programming Essentials - M35 - Iterators & Generators
Python Programming Essentials - M34 - List Comprehensions
Python Programming Essentials - M31 - PEP 8
Python Programming Essentials - M29 - Python Interpreter and Files
Python Programming Essentials - M28 - Debugging with pdb
Python Programming Essentials - M27 - Logging module
Python Programming Essentials - M25 - os and sys modules
Python Programming Essentials - M24 - math module
Python Programming Essentials - M23 - datetime module
Python Programming Essentials - M22 - File Operations
Python Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M20 - Classes and Objects
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M18 - Modules and Packages
Python Programming Essentials - M17 - Functions
Python Programming Essentials - M16 - Control Flow Statements and Loops

Recently uploaded (20)

PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Encapsulation theory and applications.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
MYSQL Presentation for SQL database connectivity
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Machine learning based COVID-19 study performance prediction
PDF
Modernizing your data center with Dell and AMD
PPT
Teaching material agriculture food technology
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Empathic Computing: Creating Shared Understanding
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Electronic commerce courselecture one. Pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Encapsulation theory and applications.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
MYSQL Presentation for SQL database connectivity
The AUB Centre for AI in Media Proposal.docx
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Dropbox Q2 2025 Financial Results & Investor Presentation
Reach Out and Touch Someone: Haptics and Empathic Computing
Machine learning based COVID-19 study performance prediction
Modernizing your data center with Dell and AMD
Teaching material agriculture food technology
NewMind AI Monthly Chronicles - July 2025
Diabetes mellitus diagnosis method based random forest with bat algorithm
Empathic Computing: Creating Shared Understanding
Encapsulation_ Review paper, used for researhc scholars
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Electronic commerce courselecture one. Pdf

Python Programming Essentials - M12 - Lists

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