SlideShare a Scribd company logo
Lists in Python
Md. Shafiuzzaman
Lecturer, Dept. of CSE, JUST
Why list?
The List Data Type
• A list is a value that contains multiple values in an ordered sequence
• A list value looks like this: ['cat', 'bat', 'rat', 'elephant']
• Values inside the list are also called items
Getting Individual Values in a List with Indexes
Getting Individual Values in a List with Indexes
• Python will give you an IndexError error message if you use an index
that exceeds the number of values in your list value
• Indexes can be only integer values, not floats
Lists of lists
Negative Indexes
Getting Sublists with Slices
• A slice can get several values from a list, in the form of a new list
Getting Sublists with Slices
Getting a List’s Length
Changing Values in a List with Indexes
List Concatenation and List Replication
Removing Values from Lists
Using for Loops with Lists
Using for Loops with Lists
Using for Loops with Lists
Exercise
Solution
The in and not in Operators
• You can determine whether a value is or isn’t in a list with the in and
not in operators.
The in and not in Operators
Multiple Assignment
Augmented Assignment Operators
Methods
• A method is the same thing as a function, except it is “called on” a
value
• Example: spam.index('hello')
• Each data type has its own set of methods
• List data type has several methods for finding, adding, removing, and
otherwise manipulating values in a list
Finding a Value in a List with the index()
Method
Finding a Value in a List with the index()
Method
• When there are duplicates of the value in the list, the index of its first
appearance is returned
Adding Values to Lists: append()
Adding Values to Lists: insert()
Removing Values from Lists with remove()
Removing Values from Lists with remove()
Removing Values from Lists with remove()
del statement vs remove()
• del statement is good to use when you know the index of the value
you want to remove from the list
• remove() method is good when you know the value you want to
remove from the list
Practice Projects: Comma Code
Sorting the Values in a List with the sort()
Method
Sorting the Values in a List with the sort()
Method
Sorting the Values in a List with the sort()
Method
• sort() method sorts the list in place; don’t try to capture the return
value by writing code like spam = spam.sort()
• you cannot sort lists that have both number values and string values
in them
Sorting the Values in a List with the sort()
Method
sort() uses “ASCIIbetical order” rather than actual alphabetical
order for sorting strings
Sort the values in regular alphabetical order
List-like Types: Strings
List-like Types: Strings
• Store your name in name variable and type the following code
Lists vs String
• List -> mutable: It can have values added, removed, or changed
• String -> immutable: It cannot be changed
“mutate” a string: slicing and concatenation
The Tuple Data Type
• Tuples are immutable
• Tuples cannot have their values modified, appended, or removed
Lists in Python
Converting Types with the list() and tuple()
Functions
References
References
Passing References
Copy Module’s copy() and deepcopy()
Functions

More Related Content

PPTX
Introduction to lists
PPTX
An Introduction To Python - Tables, List Algorithms
PPTX
Lists and loops
PPTX
Lists methods
PPTX
Arrays
PPT
Ap Power Point Chpt9
DOCX
C# Collection classes
PDF
List Manipulation in FME
Introduction to lists
An Introduction To Python - Tables, List Algorithms
Lists and loops
Lists methods
Arrays
Ap Power Point Chpt9
C# Collection classes
List Manipulation in FME

What's hot (20)

PPT
List data structure
PDF
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
PDF
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
PPTX
Lesson 11 one dimensional array
PPTX
Arrays in C++
PDF
Java Generics wildcards
PPT
Ap Power Point Chpt6
PDF
Python list functions
PPTX
Arrays
 
PPTX
Arrays and linked lists
PPTX
Data structures: linear lists
PDF
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
PPTX
How to Create an Array & types in PHP
PPT
Ppt lesson 12
PPT
Programming Logic and Design: Arrays
PPTX
Data Structures (CS8391)
PPT
Ch 8 data structures in alice
PPTX
Relational databases
PDF
Database Indexes
List data structure
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
Lesson 11 one dimensional array
Arrays in C++
Java Generics wildcards
Ap Power Point Chpt6
Python list functions
Arrays
 
Arrays and linked lists
Data structures: linear lists
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
How to Create an Array & types in PHP
Ppt lesson 12
Programming Logic and Design: Arrays
Data Structures (CS8391)
Ch 8 data structures in alice
Relational databases
Database Indexes
Ad

Similar to Lists in Python (20)

PPTX
MODULE-2.pptx
PPTX
Working with arrays in php
PPTX
Lists on the pyhton to learn the children more easily on easy codes.pptx
PPTX
Python - List, Dictionaries, Tuples,Sets
PPTX
ARRAYS.pptx
PDF
List , tuples, dictionaries and regular expressions in python
PPTX
PA1 lists
PPTX
ACP-arrays.pptx
PPTX
Array ppt
PDF
Array.pdf
PPTX
Arrays In C++
PDF
FINAL revised LIST in Python.pdf
PDF
13- Data and Its Types presentation kafss
PPTX
LM 28 - List (Operations, Slice, Methods).pptx
PPTX
MODUL new hlgjg thaybkhvnghgpv7E_02.pptx
PPTX
11 Introduction to lists.pptx
PPTX
"Sequences in Python include list, tuple, string, and range"
PDF
Python Basics it will teach you about data types
PPTX
Python introduction data structures lists etc
PPTX
Nested lists (Two dimensional lists for Python)
MODULE-2.pptx
Working with arrays in php
Lists on the pyhton to learn the children more easily on easy codes.pptx
Python - List, Dictionaries, Tuples,Sets
ARRAYS.pptx
List , tuples, dictionaries and regular expressions in python
PA1 lists
ACP-arrays.pptx
Array ppt
Array.pdf
Arrays In C++
FINAL revised LIST in Python.pdf
13- Data and Its Types presentation kafss
LM 28 - List (Operations, Slice, Methods).pptx
MODUL new hlgjg thaybkhvnghgpv7E_02.pptx
11 Introduction to lists.pptx
"Sequences in Python include list, tuple, string, and range"
Python Basics it will teach you about data types
Python introduction data structures lists etc
Nested lists (Two dimensional lists for Python)
Ad

More from Md. Shafiuzzaman Hira (20)

PPTX
Introduction to Web development
PPTX
Software measurement and estimation
PPTX
Why do we test software?
PPT
Software Requirements engineering
PPTX
Software architectural patterns
PPTX
Class based modeling
PPTX
Class diagram
PPTX
State diagram
PDF
Use case Modeling
PDF
User stories
PDF
PDF
Agile Methodology
PDF
Software Process Model
PDF
Introduction to Software Engineering Course
PPTX
PPTX
C pointers
PPTX
C structures
PPTX
How to Create Python scripts
PPTX
Regular expressions using Python
PPTX
Password locker project
Introduction to Web development
Software measurement and estimation
Why do we test software?
Software Requirements engineering
Software architectural patterns
Class based modeling
Class diagram
State diagram
Use case Modeling
User stories
Agile Methodology
Software Process Model
Introduction to Software Engineering Course
C pointers
C structures
How to Create Python scripts
Regular expressions using Python
Password locker project

Recently uploaded (20)

PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Encapsulation theory and applications.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Big Data Technologies - Introduction.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Modernizing your data center with Dell and AMD
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
 
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
cuic standard and advanced reporting.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Encapsulation theory and applications.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Mobile App Security Testing_ A Comprehensive Guide.pdf
Empathic Computing: Creating Shared Understanding
Big Data Technologies - Introduction.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Digital-Transformation-Roadmap-for-Companies.pptx
Modernizing your data center with Dell and AMD
CIFDAQ's Market Insight: SEC Turns Pro Crypto
 
NewMind AI Monthly Chronicles - July 2025
cuic standard and advanced reporting.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf

Lists in Python