SlideShare una empresa de Scribd logo
1 de 39
 List is a sequence of values called items or elements.
 The elements can be of any data type.
 The list is a most versatile data type available in
Python which can be written as a list of comma-
separated values (items) between square brackets.
 List are mutable, meaning, their elements can be
changed.
Method -1 without constructor
# empty list
my_list = []
# list of integers
my_list = [1, 2, 3]
# list with mixed datatypes
my_list = [1, "Hello", 3.4]
# nested list
my_list = [“welcome", [8, 4, 6]]
Method-2 using list constructor
# empty list
my_list = list()
# list of integers
my_list = list([1, 2, 3])
• In Python programming, a list is created by placing all the items
(elements) inside a square bracket [ ], separated by commas.
• It can have any number of items and they may be of different types
(integer, float, string etc.).
 Index operator [] is used to access an item in a list. Index starts from
0
 marks=[90,80,50,70,60]
 print(marks[0])
Output: 90
 Nested list:
 my_list = [“welcome", [8, 4, 6]]
 Print(marks[1][0])
Output: 8
 Python allows negative indexing for its sequences. The index of -
1 refers to the last item, -2 to the second last item and so on
my_list = ['p','r','o','b','e']
# Output: e
print(my_list[-1])
# Output: p
print(my_list[-5])
#Change Elements
= operator with index
>>> marks=[90,60,80]
>>> print(marks)
[90, 60, 80]
>>> marks[1]=100
>>> print(marks)
[90, 100, 80]
#Add Elements
 add one item to a list using append()
method
 add several items using extend()
 insert one item at a desired location by
using the method insert()
>>> marks.append(50)
>>> print(marks)
[90, 100, 80, 50]
>>> marks.extend([60,80,70])
>>> print(marks)
[90, 100, 80, 50, 60, 80, 70]
>>> marks.insert(3,40)
>>> print(marks)
[90, 100, 80, 40, 50, 60, 80, 70]
 delete one or more items from a
list using the keyword del.
 It can even delete the list entirely.
>>> print(marks)
[90, 100, 80, 40, 50, 60, 80, 70]
>>> del marks[6]
>>> print(marks)
[90, 100, 80, 40, 50, 60, 70]
>>> del marks
>>> print(marks)
Name Error: name 'marks' is not
defined
 clear() method to empty a list.
>>> marks.clear()
>>> print(marks)
[]
 remove() method to remove the given item
>>> marks=[90,60,80]
>>> marks.remove(80)
>>> print(marks)
[90, 60]
>>> marks.remove(100)
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
marks.remove(100)
ValueError: list.remove(x): x not in list
 pop() method to remove an item at the given index.
>>> marks=[100,20,30]
>>> marks.pop()
30
>>> print(marks)
[100, 20]
>>> >>> marks.pop(0)
100
>>> print(marks)
[20]
delete items in a list
by assigning an
empty list to a slice o
elements.
marks=[100,20,30]
>>> marks[1:2]=[]
>>> print(marks)
[100, 30]
 >>> marks.extend([40,50,60,70])
 >>> marks
 [90, 40, 50, 60, 70]
 >>> marks.pop()
 70
 >>> marks.pop()
 60
 Slicing [::] (i.e) list[start:stop:step]
 Concatenation = +
 Repetition= *
 Membership = in
 Identity = is
append() - Add an element to the end of the list
extend() - Add all elements of a list to the another list
insert() - Insert an item at the defined index
remove() - Removes an item from the list
pop() - Removes and returns an element at the given index
clear() - Removes all items from the list
index() - Returns the index of the first matched item
count() - Returns the count of number of items passed as an argument
sort() - Sort items in a list in ascending order
reverse() - Reverse the order of items in the list
copy() - Returns a shallow copy of the list
Function Description
all()
Return True if all elements of the list are true (or if the list is
empty).
any()
Return True if any element of the list is true. If the list is empty,
return False.
enumerate()
Return an enumerate object. It contains the index and value of all
the items of list as a tuple.
len() Return the length (the number of items) in the list.
list() Convert an iterable (tuple, string, set, dictionary) to a list.
max() Return the largest item in the list.
min() Return the smallest item in the list
sorted() Return a new sorted list (does not sort the list itself).
sum() Return the sum of all elements in the list.
copy()
reversed()

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Chapter 17 Tuples
Chapter 17 TuplesChapter 17 Tuples
Chapter 17 Tuples
 
Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
single linked list
single linked listsingle linked list
single linked list
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
stack & queue
stack & queuestack & queue
stack & queue
 
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentation
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
Sets in python
Sets in pythonSets in python
Sets in python
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Python strings
Python stringsPython strings
Python strings
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
 

Similar a List in Python

Python Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdfPython Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdf
MCCMOTOR
 

Similar a List in Python (20)

Python lists &amp; sets
Python lists &amp; setsPython lists &amp; sets
Python lists &amp; sets
 
Lecture2.pptx
Lecture2.pptxLecture2.pptx
Lecture2.pptx
 
Module-2.pptx
Module-2.pptxModule-2.pptx
Module-2.pptx
 
Python list
Python listPython list
Python list
 
List_tuple_dictionary.pptx
List_tuple_dictionary.pptxList_tuple_dictionary.pptx
List_tuple_dictionary.pptx
 
List Data Structure.docx
List Data Structure.docxList Data Structure.docx
List Data Structure.docx
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Python for Beginners(v3)
Python for Beginners(v3)Python for Beginners(v3)
Python for Beginners(v3)
 
Python PCEP Lists Collections of Data
Python PCEP Lists Collections of DataPython PCEP Lists Collections of Data
Python PCEP Lists Collections of Data
 
Python-List.pptx
Python-List.pptxPython-List.pptx
Python-List.pptx
 
python_avw - Unit-03.pdf
python_avw - Unit-03.pdfpython_avw - Unit-03.pdf
python_avw - Unit-03.pdf
 
The Ring programming language version 1.9 book - Part 29 of 210
The Ring programming language version 1.9 book - Part 29 of 210The Ring programming language version 1.9 book - Part 29 of 210
The Ring programming language version 1.9 book - Part 29 of 210
 
Python Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdfPython Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdf
 
The Ring programming language version 1.8 book - Part 27 of 202
The Ring programming language version 1.8 book - Part 27 of 202The Ring programming language version 1.8 book - Part 27 of 202
The Ring programming language version 1.8 book - Part 27 of 202
 
Lists_tuples.pptx
Lists_tuples.pptxLists_tuples.pptx
Lists_tuples.pptx
 
Pytho_tuples
Pytho_tuplesPytho_tuples
Pytho_tuples
 
Unit 4 python -list methods
Unit 4   python -list methodsUnit 4   python -list methods
Unit 4 python -list methods
 
Data type list_methods_in_python
Data type list_methods_in_pythonData type list_methods_in_python
Data type list_methods_in_python
 
16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 

Más de Siddique Ibrahim

Más de Siddique Ibrahim (20)

Python Control structures
Python Control structuresPython Control structures
Python Control structures
 
Python programming introduction
Python programming introductionPython programming introduction
Python programming introduction
 
Data mining basic fundamentals
Data mining basic fundamentalsData mining basic fundamentals
Data mining basic fundamentals
 
Basic networking
Basic networkingBasic networking
Basic networking
 
Virtualization Concepts
Virtualization ConceptsVirtualization Concepts
Virtualization Concepts
 
Networking devices(siddique)
Networking devices(siddique)Networking devices(siddique)
Networking devices(siddique)
 
Osi model 7 Layers
Osi model 7 LayersOsi model 7 Layers
Osi model 7 Layers
 
Mysql grand
Mysql grandMysql grand
Mysql grand
 
Getting started into mySQL
Getting started into mySQLGetting started into mySQL
Getting started into mySQL
 
pipelining
pipeliningpipelining
pipelining
 
Micro programmed control
Micro programmed controlMicro programmed control
Micro programmed control
 
Hardwired control
Hardwired controlHardwired control
Hardwired control
 
interface
interfaceinterface
interface
 
Interrupt
InterruptInterrupt
Interrupt
 
Interrupt
InterruptInterrupt
Interrupt
 
DMA
DMADMA
DMA
 
Io devies
Io deviesIo devies
Io devies
 
Stack & queue
Stack & queueStack & queue
Stack & queue
 
Metadata in data warehouse
Metadata in data warehouseMetadata in data warehouse
Metadata in data warehouse
 
Data extraction, transformation, and loading
Data extraction, transformation, and loadingData extraction, transformation, and loading
Data extraction, transformation, and loading
 

Último

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 

List in Python

  • 1.
  • 2.  List is a sequence of values called items or elements.  The elements can be of any data type.  The list is a most versatile data type available in Python which can be written as a list of comma- separated values (items) between square brackets.  List are mutable, meaning, their elements can be changed.
  • 3. Method -1 without constructor # empty list my_list = [] # list of integers my_list = [1, 2, 3] # list with mixed datatypes my_list = [1, "Hello", 3.4] # nested list my_list = [“welcome", [8, 4, 6]] Method-2 using list constructor # empty list my_list = list() # list of integers my_list = list([1, 2, 3]) • In Python programming, a list is created by placing all the items (elements) inside a square bracket [ ], separated by commas. • It can have any number of items and they may be of different types (integer, float, string etc.).
  • 4.
  • 5.  Index operator [] is used to access an item in a list. Index starts from 0  marks=[90,80,50,70,60]  print(marks[0]) Output: 90  Nested list:  my_list = [“welcome", [8, 4, 6]]  Print(marks[1][0]) Output: 8
  • 6.  Python allows negative indexing for its sequences. The index of - 1 refers to the last item, -2 to the second last item and so on my_list = ['p','r','o','b','e'] # Output: e print(my_list[-1]) # Output: p print(my_list[-5])
  • 7. #Change Elements = operator with index >>> marks=[90,60,80] >>> print(marks) [90, 60, 80] >>> marks[1]=100 >>> print(marks) [90, 100, 80] #Add Elements  add one item to a list using append() method  add several items using extend()  insert one item at a desired location by using the method insert() >>> marks.append(50) >>> print(marks) [90, 100, 80, 50] >>> marks.extend([60,80,70]) >>> print(marks) [90, 100, 80, 50, 60, 80, 70] >>> marks.insert(3,40) >>> print(marks) [90, 100, 80, 40, 50, 60, 80, 70]
  • 8.  delete one or more items from a list using the keyword del.  It can even delete the list entirely. >>> print(marks) [90, 100, 80, 40, 50, 60, 80, 70] >>> del marks[6] >>> print(marks) [90, 100, 80, 40, 50, 60, 70] >>> del marks >>> print(marks) Name Error: name 'marks' is not defined  clear() method to empty a list. >>> marks.clear() >>> print(marks) []  remove() method to remove the given item >>> marks=[90,60,80] >>> marks.remove(80) >>> print(marks) [90, 60] >>> marks.remove(100) Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> marks.remove(100) ValueError: list.remove(x): x not in list  pop() method to remove an item at the given index. >>> marks=[100,20,30] >>> marks.pop() 30 >>> print(marks) [100, 20] >>> >>> marks.pop(0) 100 >>> print(marks) [20] delete items in a list by assigning an empty list to a slice o elements. marks=[100,20,30] >>> marks[1:2]=[] >>> print(marks) [100, 30]
  • 9.  >>> marks.extend([40,50,60,70])  >>> marks  [90, 40, 50, 60, 70]  >>> marks.pop()  70  >>> marks.pop()  60
  • 10.  Slicing [::] (i.e) list[start:stop:step]  Concatenation = +  Repetition= *  Membership = in  Identity = is
  • 11.
  • 12. append() - Add an element to the end of the list extend() - Add all elements of a list to the another list insert() - Insert an item at the defined index remove() - Removes an item from the list pop() - Removes and returns an element at the given index clear() - Removes all items from the list index() - Returns the index of the first matched item count() - Returns the count of number of items passed as an argument sort() - Sort items in a list in ascending order reverse() - Reverse the order of items in the list copy() - Returns a shallow copy of the list
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39. Function Description all() Return True if all elements of the list are true (or if the list is empty). any() Return True if any element of the list is true. If the list is empty, return False. enumerate() Return an enumerate object. It contains the index and value of all the items of list as a tuple. len() Return the length (the number of items) in the list. list() Convert an iterable (tuple, string, set, dictionary) to a list. max() Return the largest item in the list. min() Return the smallest item in the list sorted() Return a new sorted list (does not sort the list itself). sum() Return the sum of all elements in the list. copy() reversed()