SlideShare a Scribd company logo
Python list
 Creating List
 Access, Modify and Delete list Elements
 Merge Lists
 Use the slicing syntax to operate on
sublists
 Loop over lists
 A list is a compound data type, you can group values
together
 A list contains items separated by commas and enclosed
within square brackets ([]).
 Lists are similar to arrays in C.
 One difference between them is that the items belonging
to a list can be of different data type.
 The values stored in a list can be accessed using the [ ]
operator
 Index starts at 0.
Python list
Python list
 What is the result of this code?
nums = [5, 4, 3, 2, 1]
print(nums[-2])
>>> math = 45
>>> science = 23
>>> social = 28
>>> marksList = [math, science, social]
>>>
>>> print(marksList)
[45, 23, 28]
 Create a list, areas, that contains the area of the hallway (hall), kitchen
(kit), living room (liv), bedroom (bed) and bathroom (bath), in this order.
Use the predefined variables.
 Print areas with the print() function.
# area variables (in square meters)
hall = 11.25
kit = 18.0
liv = 20.0
bed = 10.75
bath = 9.50
# Create list areas
_____________________________
# Print areas
_____________________________
SN Function with Description
1 len(list)
Gives the total length of the list.
2 max(list)
Returns item from the list with max value.
3 min(list)
Returns item from the list with min value.
4. sum(list)
Returns the sum of all elements of list
Python list
Python list
 Two List can be combined using +
operator.
 We cannot add a integer with list using +
 Forming new lists with a repeating sequence using the
multiplication operator:
 Create a list of 5 elements with initial value as 0
Insertion
Append(ele) Add element at end of List
Insert(index , ele) Add element at given index
Extend( seq ) Appends all the elements of seq to list
Deletion Pop(index) Delete element based on Index
Remove(key) Delete element based on key
Count(key) Returns the number of occurrences of key
Index(key) Returns the index of key element
Sort Sorts elements of List
Reverse Reverses elements of list
 We can add elements to list.
 To add one element, use append method.
 This adds an item to the end of an existing list.
mylist = []
mylist.append(5)
mylist.append(8)
mylist.append(12)
#prints [5,8,12]
print(mylist)
Python list
insert(index, element)
 Insert the elements in sorted order.
 Algorithm
 If list is empty, append element to list.
 If element is less than first element, insert front.
 Traverse till current element is less than
element to insert or till end of list
 Insert the element.
 We can add a series of elements using extend
method or + operator.
 a = [1,2,3]
 b = [4,5,6]
 Understand the difference between
 a.append(b) and
 a.extend(b)
 To delete last element
 To delete element at specific Index
 Remove method takes the key element to
delete and removes it if present.
Python list
Python list
Python list
Python list
Python list
 In all the previous methods, we were not able
to determine the index where the key
element is present.
 index() method finds the given element in
a list and returns its position.
 If the same element is present more than
once, index() method returns its
smallest/first position.
 If not found, it raises
a ValueError exception indicating the
element is not in the list.
 The index method finds the first occurrence of a list item and
returns its index.
If the item isn't in the list, it raises aValueError.
 letters = ['p', 'q', 'r', 's', 'p', 'u']
 print(letters.index('r'))
 print(letters.index('p'))
 print(letters.index('z'))
2
0
ValueError: 'z' is not in list
Python list
Python list
 The sort method can be used to sort the
elements of list.
Python list
Python list
Python list
 mylist = [5 , 8 , 12 , 20 , 25, 50]
 mylist[startIndex : endIndex]
Python list
 Mylist[startIndex : endIndex : step]
 mylist = [5 , 8 , 12 , 20 , 25, 50]
 print('Full list elements' , mylist)
 print('printing alternate elements', mylist[ : : 2])
 print('printing in reverse order ', mylist[ : : -1])
Python list
 == operator is used to check if two list have
same elements.
 is operator is used to check if two references
refer to same list.
Python list
Python list
 # area variables (in square meters)
 hall = 11.25
 kit = 18.0
 liv = 20.0
 bed = 10.75
 bath = 9.50
 # house information as list of lists
 house = [["hallway", hall],
 ["kitchen", kit],
 ["living room", liv],
 ["bedroom",bed],
 ["bathroom",bath]]
 # Print out house
 print(house)
 print(house[0])
 print(house[1][0]) [['hallway', 11.25], ['kitchen', 18.0], ['living room', 20.0], ['bedroom', 10.75], ['bathroom', 9.5]]
['hallway', 11.25]
kitchen
 BMI = weight/height2
 height = [ 1.87, 1.87, 1.82, 1.91, 1.90, 1.85]
 weight = [81.65, 97.52, 95.25, 92.98, 86.18, 88.45]
 bmi = weight / (height ** 2)
 BMI = weight/height2
 height = [ 1.87, 1.87, 1.82, 1.91, 1.90, 1.85]
 weight = [81.65, 97.52, 95.25, 92.98, 86.18, 88.45]
 bmi = []
 n = len(height)
 for i in range(0,n):
 bmi.append(weight[i] / (height[i] ** 2))
 print (bmi)
 Python List
 Convert a list of temperature values from
celsius to fahrenheit
 Given a list of elements, create a new list which
has elements with one added to every element
of old list.
 nums = [12, 8, 21, 3, 16]
 new_nums = [13, 9, 22, 4, 17]
 Collapse for loops for building lists into a
single line
 Components
 Iterable
 Iterator variable
 Output expression
Python list
Python list
 Given a list of elements extract all the odd
elements and place in new list.
Python list
 Conditionals on the iterable
 [num ** 2 for num in range(10) if num % 2 == 0]
 Conditionals on the output expression
 [num ** 2 if num % 2 == 0 else 0 for num in
range(10)]
 The all() function returns True if all elements of the
supplied iterable are true or if there are no elements.
 So if all elements in a list, tuple, or set match Python's
definition of being true, then all() returns True.
 The any() function is the converse of
the all() function. any() returns True if any element of
the iterable evaluates true.
 If the iterable is empty, the function returns False
 Read the marks of subject and find the result.
 Print Pass if marks scored in all the subjects is
>= 35 otherwise Fail.
The zip() function take iterables, makes iterator that aggregates elements
based on the iterables passed, and returns an iterator of tuples.

More Related Content

What's hot (20)

PDF
Object oriented approach in python programming
Srinivas Narasegouda
 
PPTX
File handling in Python
Megha V
 
PPTX
Python Functions
Mohammed Sikander
 
PPT
standard template library(STL) in C++
•sreejith •sree
 
PDF
Python strings
Mohammed Sikander
 
PPTX
Unit 4 python -list methods
narmadhakin
 
PDF
Tuples in Python
DPS Ranipur Haridwar UK
 
PPTX
Looping statement in python
RaginiJain21
 
PPTX
Data Structures in Python
Devashish Kumar
 
PPTX
Python: Modules and Packages
Damian T. Gordon
 
PPTX
Python Scipy Numpy
Girish Khanzode
 
PDF
Set methods in python
deepalishinkar1
 
PDF
Strings in python
Prabhakaran V M
 
PDF
Python programming : Arrays
Emertxe Information Technologies Pvt Ltd
 
PPTX
Python-Classes.pptx
Karudaiyar Ganapathy
 
PPSX
Data Structure (Queue)
Adam Mukharil Bachtiar
 
PPT
Introduction to Python
Nowell Strite
 
PPTX
Data Analysis in Python-NumPy
Devashish Kumar
 
PDF
Python programming : List and tuples
Emertxe Information Technologies Pvt Ltd
 
Object oriented approach in python programming
Srinivas Narasegouda
 
File handling in Python
Megha V
 
Python Functions
Mohammed Sikander
 
standard template library(STL) in C++
•sreejith •sree
 
Python strings
Mohammed Sikander
 
Unit 4 python -list methods
narmadhakin
 
Tuples in Python
DPS Ranipur Haridwar UK
 
Looping statement in python
RaginiJain21
 
Data Structures in Python
Devashish Kumar
 
Python: Modules and Packages
Damian T. Gordon
 
Python Scipy Numpy
Girish Khanzode
 
Set methods in python
deepalishinkar1
 
Strings in python
Prabhakaran V M
 
Python programming : Arrays
Emertxe Information Technologies Pvt Ltd
 
Python-Classes.pptx
Karudaiyar Ganapathy
 
Data Structure (Queue)
Adam Mukharil Bachtiar
 
Introduction to Python
Nowell Strite
 
Data Analysis in Python-NumPy
Devashish Kumar
 
Python programming : List and tuples
Emertxe Information Technologies Pvt Ltd
 

Similar to Python list (20)

PDF
Python lists & sets
Aswini Dharmaraj
 
PPTX
list in python and traversal of list.pptx
urvashipundir04
 
PDF
Python Unit 5 Questions n Notes.pdf
MCCMOTOR
 
PPTX
PYTHON-PROGRAMMING-UNIT-III.pptx kghbg kfhjf jruufg jtuuf
DeepakRattan3
 
PPTX
Chapter 3-Data structure in python programming.pptx
atharvdeshpande20
 
PPTX
Lecture2.pptx
Sakith1
 
PDF
python_avw - Unit-03.pdf
AshaWankar1
 
PDF
GE3151_PSPP_UNIT_4_Notes
Guru Nanak Technical Institutions
 
PPTX
Python list tuple dictionary presentation
NarendraDev11
 
PPTX
List_tuple_dictionary.pptx
ChandanVatsa2
 
PPTX
Python PCEP Lists Collections of Data
IHTMINSTITUTE
 
PPTX
python ..... _
swati463221
 
PPTX
institute of techonolgy and education tr
sekharuppuluri
 
PPTX
List_tuple_dictionary.pptxList_tuple_dictionary.pptx
usha raj
 
PPTX
Module-2.pptx
GaganRaj28
 
PPT
9781439035665 ppt ch09
Terry Yoast
 
PPTX
Python for the data science most in cse.pptx
Rajasekhar364622
 
PDF
The Ring programming language version 1.8 book - Part 27 of 202
Mahmoud Samir Fayed
 
PPTX
Python _dataStructures_ List, Tuples, its functions
VidhyaB10
 
PPTX
Lists.pptx
Yagna15
 
Python lists & sets
Aswini Dharmaraj
 
list in python and traversal of list.pptx
urvashipundir04
 
Python Unit 5 Questions n Notes.pdf
MCCMOTOR
 
PYTHON-PROGRAMMING-UNIT-III.pptx kghbg kfhjf jruufg jtuuf
DeepakRattan3
 
Chapter 3-Data structure in python programming.pptx
atharvdeshpande20
 
Lecture2.pptx
Sakith1
 
python_avw - Unit-03.pdf
AshaWankar1
 
GE3151_PSPP_UNIT_4_Notes
Guru Nanak Technical Institutions
 
Python list tuple dictionary presentation
NarendraDev11
 
List_tuple_dictionary.pptx
ChandanVatsa2
 
Python PCEP Lists Collections of Data
IHTMINSTITUTE
 
python ..... _
swati463221
 
institute of techonolgy and education tr
sekharuppuluri
 
List_tuple_dictionary.pptxList_tuple_dictionary.pptx
usha raj
 
Module-2.pptx
GaganRaj28
 
9781439035665 ppt ch09
Terry Yoast
 
Python for the data science most in cse.pptx
Rajasekhar364622
 
The Ring programming language version 1.8 book - Part 27 of 202
Mahmoud Samir Fayed
 
Python _dataStructures_ List, Tuples, its functions
VidhyaB10
 
Lists.pptx
Yagna15
 
Ad

More from Mohammed Sikander (20)

PPTX
Strings in C - covers string functions
Mohammed Sikander
 
PDF
Smart Pointers, Modern Memory Management Techniques
Mohammed Sikander
 
PDF
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
PDF
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Mohammed Sikander
 
PDF
Operator Overloading in C++
Mohammed Sikander
 
PDF
Python_Regular Expression
Mohammed Sikander
 
PPTX
Modern_CPP-Range-Based For Loop.pptx
Mohammed Sikander
 
PDF
Modern_cpp_auto.pdf
Mohammed Sikander
 
PDF
Python exception handling
Mohammed Sikander
 
PDF
Python Flow Control
Mohammed Sikander
 
PDF
Introduction to Python
Mohammed Sikander
 
PPTX
Pointer basics
Mohammed Sikander
 
PPTX
Signal
Mohammed Sikander
 
PPTX
File management
Mohammed Sikander
 
PPT
Functions in C++
Mohammed Sikander
 
PPT
CPP Language Basics - Reference
Mohammed Sikander
 
PPTX
Java arrays
Mohammed Sikander
 
PPTX
Java strings
Mohammed Sikander
 
PPTX
Java notes 1 - operators control-flow
Mohammed Sikander
 
Strings in C - covers string functions
Mohammed Sikander
 
Smart Pointers, Modern Memory Management Techniques
Mohammed Sikander
 
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Mohammed Sikander
 
Operator Overloading in C++
Mohammed Sikander
 
Python_Regular Expression
Mohammed Sikander
 
Modern_CPP-Range-Based For Loop.pptx
Mohammed Sikander
 
Modern_cpp_auto.pdf
Mohammed Sikander
 
Python exception handling
Mohammed Sikander
 
Python Flow Control
Mohammed Sikander
 
Introduction to Python
Mohammed Sikander
 
Pointer basics
Mohammed Sikander
 
File management
Mohammed Sikander
 
Functions in C++
Mohammed Sikander
 
CPP Language Basics - Reference
Mohammed Sikander
 
Java arrays
Mohammed Sikander
 
Java strings
Mohammed Sikander
 
Java notes 1 - operators control-flow
Mohammed Sikander
 
Ad

Recently uploaded (20)

PDF
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 

Python list

  • 2.  Creating List  Access, Modify and Delete list Elements  Merge Lists  Use the slicing syntax to operate on sublists  Loop over lists
  • 3.  A list is a compound data type, you can group values together  A list contains items separated by commas and enclosed within square brackets ([]).  Lists are similar to arrays in C.  One difference between them is that the items belonging to a list can be of different data type.  The values stored in a list can be accessed using the [ ] operator  Index starts at 0.
  • 6.  What is the result of this code? nums = [5, 4, 3, 2, 1] print(nums[-2])
  • 7. >>> math = 45 >>> science = 23 >>> social = 28 >>> marksList = [math, science, social] >>> >>> print(marksList) [45, 23, 28]
  • 8.  Create a list, areas, that contains the area of the hallway (hall), kitchen (kit), living room (liv), bedroom (bed) and bathroom (bath), in this order. Use the predefined variables.  Print areas with the print() function. # area variables (in square meters) hall = 11.25 kit = 18.0 liv = 20.0 bed = 10.75 bath = 9.50 # Create list areas _____________________________ # Print areas _____________________________
  • 9. SN Function with Description 1 len(list) Gives the total length of the list. 2 max(list) Returns item from the list with max value. 3 min(list) Returns item from the list with min value. 4. sum(list) Returns the sum of all elements of list
  • 12.  Two List can be combined using + operator.  We cannot add a integer with list using +
  • 13.  Forming new lists with a repeating sequence using the multiplication operator:  Create a list of 5 elements with initial value as 0
  • 14. Insertion Append(ele) Add element at end of List Insert(index , ele) Add element at given index Extend( seq ) Appends all the elements of seq to list Deletion Pop(index) Delete element based on Index Remove(key) Delete element based on key Count(key) Returns the number of occurrences of key Index(key) Returns the index of key element Sort Sorts elements of List Reverse Reverses elements of list
  • 15.  We can add elements to list.  To add one element, use append method.  This adds an item to the end of an existing list. mylist = [] mylist.append(5) mylist.append(8) mylist.append(12) #prints [5,8,12] print(mylist)
  • 18.  Insert the elements in sorted order.  Algorithm  If list is empty, append element to list.  If element is less than first element, insert front.  Traverse till current element is less than element to insert or till end of list  Insert the element.
  • 19.  We can add a series of elements using extend method or + operator.
  • 20.  a = [1,2,3]  b = [4,5,6]  Understand the difference between  a.append(b) and  a.extend(b)
  • 21.  To delete last element
  • 22.  To delete element at specific Index
  • 23.  Remove method takes the key element to delete and removes it if present.
  • 29.  In all the previous methods, we were not able to determine the index where the key element is present.
  • 30.  index() method finds the given element in a list and returns its position.  If the same element is present more than once, index() method returns its smallest/first position.  If not found, it raises a ValueError exception indicating the element is not in the list.
  • 31.  The index method finds the first occurrence of a list item and returns its index. If the item isn't in the list, it raises aValueError.  letters = ['p', 'q', 'r', 's', 'p', 'u']  print(letters.index('r'))  print(letters.index('p'))  print(letters.index('z')) 2 0 ValueError: 'z' is not in list
  • 34.  The sort method can be used to sort the elements of list.
  • 38.  mylist = [5 , 8 , 12 , 20 , 25, 50]  mylist[startIndex : endIndex]
  • 40.  Mylist[startIndex : endIndex : step]  mylist = [5 , 8 , 12 , 20 , 25, 50]  print('Full list elements' , mylist)  print('printing alternate elements', mylist[ : : 2])  print('printing in reverse order ', mylist[ : : -1])
  • 42.  == operator is used to check if two list have same elements.  is operator is used to check if two references refer to same list.
  • 45.  # area variables (in square meters)  hall = 11.25  kit = 18.0  liv = 20.0  bed = 10.75  bath = 9.50  # house information as list of lists  house = [["hallway", hall],  ["kitchen", kit],  ["living room", liv],  ["bedroom",bed],  ["bathroom",bath]]  # Print out house  print(house)  print(house[0])  print(house[1][0]) [['hallway', 11.25], ['kitchen', 18.0], ['living room', 20.0], ['bedroom', 10.75], ['bathroom', 9.5]] ['hallway', 11.25] kitchen
  • 46.  BMI = weight/height2  height = [ 1.87, 1.87, 1.82, 1.91, 1.90, 1.85]  weight = [81.65, 97.52, 95.25, 92.98, 86.18, 88.45]  bmi = weight / (height ** 2)
  • 47.  BMI = weight/height2  height = [ 1.87, 1.87, 1.82, 1.91, 1.90, 1.85]  weight = [81.65, 97.52, 95.25, 92.98, 86.18, 88.45]  bmi = []  n = len(height)  for i in range(0,n):  bmi.append(weight[i] / (height[i] ** 2))  print (bmi)
  • 48.  Python List  Convert a list of temperature values from celsius to fahrenheit
  • 49.  Given a list of elements, create a new list which has elements with one added to every element of old list.  nums = [12, 8, 21, 3, 16]  new_nums = [13, 9, 22, 4, 17]
  • 50.  Collapse for loops for building lists into a single line  Components  Iterable  Iterator variable  Output expression
  • 53.  Given a list of elements extract all the odd elements and place in new list.
  • 55.  Conditionals on the iterable  [num ** 2 for num in range(10) if num % 2 == 0]  Conditionals on the output expression  [num ** 2 if num % 2 == 0 else 0 for num in range(10)]
  • 56.  The all() function returns True if all elements of the supplied iterable are true or if there are no elements.  So if all elements in a list, tuple, or set match Python's definition of being true, then all() returns True.
  • 57.  The any() function is the converse of the all() function. any() returns True if any element of the iterable evaluates true.  If the iterable is empty, the function returns False
  • 58.  Read the marks of subject and find the result.  Print Pass if marks scored in all the subjects is >= 35 otherwise Fail.
  • 59. The zip() function take iterables, makes iterator that aggregates elements based on the iterables passed, and returns an iterator of tuples.