SlideShare a Scribd company logo
1 of 26
Download to read offline
LIST
Creating List
• Lists in Python can be created by just placing the sequence inside the
square brackets[ ].
• Creating list:
• Initial black list
• List with the use of String
• Use of Multiple Values
• Multi-dimesional list
4/3/2020LIST IN PYTHON 2
List Index
â–Ş We can use the index operator [] to access an item in a list. Index starts from 0. So, a
list having 5 elements will have index from 0 to 4.
â–Ş Nested list are accessed using nested indexing.
â–Ş 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.
4/3/2020LIST IN PYTHON 3
List Method:Append
• append( ) :
➢ It is built-in function
➢ Only one element at a time can be added to the list.
➢ For addition of multiple elements with append ( ) method loops are used.
➢ Tuples,Sets,Lists can also be added to existing list with the use of append( ) method.
4/3/2020LIST IN PYTHON 4
List Method :Insert
• insert( ) :
➢ It is built-in function
➢ The insert() method inserts an element to the list at a given index.
➢ The syntax of insert() method is:
list.insert(index,element)
➢ The insert() function takes two parameters:
• index - position where an element needs to be inserted
• element - this is the element to be inserted in the list
• This method does not return anything.Return none( ).
4/3/2020LIST IN PYTHON 5
List Method :Extend
• extend ( ) :
➢ It is built-in function
➢ The extend() extends the list by adding all items of a list (passed as an argument) to
the end.
➢ The syntax of insert() method is:
list1.extend(list2)
• If you need to add elements of other native datatypes (like tuple and set) to the list.
• E.g.list1.extend(list(tuple_type))
4/3/2020LIST IN PYTHON 6
Slicing List
• We can access a range of items in a list by using the slicing operator (colon).
my_list = ['c','o','r','o','n','a','2','0','2','0']
# elements 3rd to 5th
print(my_list[2:5])
# elements beginning to 4th
print(my_list[:-5])
# elements 6th to end
print(my_list[5:])
# elements beginning to end
print(my_list[:])
C O R O N A 2 0 2 0
0 1 2 3 4 5 6 7 8 9
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
Indexing
Negative
Indexing
4/3/2020LIST IN PYTHON 7
Add element from List
• List are mutable, meaning, their elements can be changed
unlike string or tuple.
• We can use assignment operator (=) to change an item or a range of items.
# mistake values
odd = [2, 4, 6, 8]
# change the 1st item
odd[0] = 1
# Output: [1, 4, 6, 8]
print(odd)
# change 2nd to 4th items
odd[1:4] = [3, 5, 7]
# Output: [1, 3, 5, 7]
print(odd)
4/3/2020LIST IN PYTHON 8
Delete element from List
• We can delete one or more items from a list using the keyword del. It can
even delete the list entirely.
my_list = ['p','r','o','b','l','e','m']
# delete one item
del my_list[2]
print(my_list)
# delete multiple items
del my_list[1:5]
print(my_list)
# delete entire list
del my_list
print(my_list)
4/3/2020LIST IN PYTHON 9
Delete slice of items from List
• We can also delete items in a list by assigning an empty list to a slice of elements.
my_list = ['p','r','o','b','l','e','m’]
my_list[2:3] = []
print(my_list)
my_list[2:5] = []
print(my_list)
4/3/2020LIST IN PYTHON 10
Remove( ) item from List
• We can use remove() method to remove the given item
my_list = ['p','r','o','b','l','e','m']
my_list.remove('p')
# Output: ['r', 'o', 'b', 'l', 'e', 'm']
print(my_list)
4/3/2020LIST IN PYTHON 11
Pop( ) item from List
• pop() method to remove an item at the given index.
• The pop() method removes and returns the last item if index is not provided.
• This helps us implement lists as stacks (first in, last out data structure).
print(my_list.pop( ))
print(my_list)
# Output: ['p','o','b','l','e']
my_list = ['p','r','o','b','l','e','m’]
print(my_list.pop(1))
print(my_list)
# Output: ['p','o','b','l','e','m’]
4/3/2020LIST IN PYTHON 12
Clear List
• We can also use the clear() method to empty a list.
my_list = ['p','r','o','b','l','e','m’]
my_list.clear()
#Output : [ ]
4/3/2020LIST IN PYTHON 13
+ and * operator
• We can also use + operator to combine two lists. This is also called concatenation.
• The * operator repeats a list for the given number of times.
odd = [1, 3, 5]
# Output: [1, 3, 5, 9, 7, 5]
print(odd + [9, 7, 5])
#Output: ["re", "re", "re"]
print(["re"] * 3)
4/3/2020LIST IN PYTHON 14
Count ( )
• count() method returns count of how many times obj occurs in list.
• Syntax :
list_name.count(object)
•
my_list = [3, 8, 1, 6, 0, 8, 4]
# Output: 2
print(my_list.count(8))
4/3/2020LIST IN PYTHON 15
Index ( )
• Index( ) method returns the index of the first matched item.
• Syntax :
list_name.index(object)
my_list = [3, 8, 1, 6, 0, 8, 4]
# Output: 1
print(my_list.index(8))
4/3/2020LIST IN PYTHON 16
Index ( )
• Returns the index of first occurrence. Start and End index are not necessary parameters.
Syntax:
• list.index(element[,start[,end]])
List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
print(List.index(2,2))
# will check from index 2.
#Output
List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
print(List.index(2,2,6))
# will check from index 2 to 5.
#Output : 4
4/3/2020LIST IN PYTHON 17
sort( )
• The sort() method sorts the elements of a given list in a specific order - Ascending or
Descending.
• The syntax of sort() method is:
list.sort(key=..., reverse=...)
• Alternatively, you can also use Python's in-built function sorted() for the same purpose.
• sorted(list, key=..., reverse=...)
• Note: Simplest difference between sort() and sorted() is: sort() doesn't return any value
while, sorted() returns an iterable list.
4/3/2020LIST IN PYTHON 18
sort( )
• The sort function can be used to sort the list in both ascending and descending order.
• Syntax:
• Listname.sort( )
• This will srt the given list in ascending order.
• Syntax
• list_name.sort(reverse=True)
• This will sort the given list in descending order.
4/3/2020LIST IN PYTHON 19
sort( )
• To sorts according to user’s choice.
• It has two optional parameters:
reverse – If true, the list is sorted in descending order
key – function that serves as a key for the sort comparison
• It returns a sorted list according to the passed parameter.
4/3/2020LIST IN PYTHON 20
Reverse( )
• The reverse( ) method reverses the elements of a given list.
• Syntax:
listname.reverse( )
• The reverse ( ) function doesn’t take any argument.
• Reverse a list using slicing operator.
• E.g. Reverse_list=listname[::-1]
• To access individual elements of a list in reverse order,to use reversed( )
method.
4/3/2020LIST IN PYTHON 21
Copy
• The copy( ) method returns a shallow copy of the list.
• It doesn’t take any parameter.
• Syntax :
• New_list=listname.copy( )
• A list can be copied with = operator.
• Shallow copy of a list using slicing.
4/3/2020LIST IN PYTHON 22
SUM,LEN
• It returns the sums up the numbers in the list.
• Syntax:
• sum(iterable,start)
• Calculates total length of List.
• Syntax:
• len(list_name)
4/3/2020LIST IN PYTHON 23
MIN,MAX,CMP
• Calculates minimum of all the elements of List.
• Syntax:
• min(List)
• Calculates maximum of all the elements of List.
Syntax:
• max(List)
• Compares elements of two lists.
• Syntax:
• cmp(list1, list2)
4/3/2020LIST IN PYTHON 24
LIST
• list() takes sequence types and converts them to lists. This is used to
convert a given tuple into list.
• Syntax:
• list(seq)
4/3/2020LIST IN PYTHON 25
Summary
• List is a data type of container in Data Structures, which is used to store data
• Homogeneous (same)
• Hetrogeneous(different)
• List are ordered and have a definite count.
• List is mutable meaning you can change their content without changing their identity or
changeable(Item can be alter)
4/3/2020LIST IN PYTHON 26

More Related Content

What's hot

Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data StructureZidny Nafan
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Edureka!
 
Set methods in python
Set methods in pythonSet methods in python
Set methods in pythondeepalishinkar1
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in pythonCeline George
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionarySoba Arjun
 
stack & queue
stack & queuestack & queue
stack & queuemanju rani
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in pythoneShikshak
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure Janki Shah
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]Muhammad Hammad Waseem
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applicationssomendra kumar
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in PythonPooja B S
 
Python Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular ExpressionsPython Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular ExpressionsRanel Padon
 
Python GUI Programming
Python GUI ProgrammingPython GUI Programming
Python GUI ProgrammingRTS Tech
 
List , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in pythonList , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in pythonchanna basava
 

What's hot (20)

Stack
StackStack
Stack
 
List in Python
List in PythonList in Python
List in Python
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Python Programming Essentials - M12 - Lists
Python Programming Essentials - M12 - ListsPython Programming Essentials - M12 - Lists
Python Programming Essentials - M12 - Lists
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
 
Set methods in python
Set methods in pythonSet methods in python
Set methods in python
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
Python-Encapsulation.pptx
Python-Encapsulation.pptxPython-Encapsulation.pptx
Python-Encapsulation.pptx
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
 
stack & queue
stack & queuestack & queue
stack & queue
 
Python set
Python setPython set
Python set
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in Python
 
Python Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular ExpressionsPython Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular Expressions
 
Python GUI Programming
Python GUI ProgrammingPython GUI Programming
Python GUI Programming
 
List , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in pythonList , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in python
 
Tuples in Python
Tuples in PythonTuples in Python
Tuples in Python
 

Similar to Data type list_methods_in_python

Similar to Data type list_methods_in_python (20)

Python-List.pptx
Python-List.pptxPython-List.pptx
Python-List.pptx
 
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
 
updated_list.pptx
updated_list.pptxupdated_list.pptx
updated_list.pptx
 
Chapter 15 Lists
Chapter 15 ListsChapter 15 Lists
Chapter 15 Lists
 
PYTHON.pptx
PYTHON.pptxPYTHON.pptx
PYTHON.pptx
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionary
 
8 python data structure-1
8 python data structure-18 python data structure-1
8 python data structure-1
 
lists8.pptx of chapter 12 IP Basic Python
lists8.pptx of chapter 12 IP Basic Pythonlists8.pptx of chapter 12 IP Basic Python
lists8.pptx of chapter 12 IP Basic Python
 
Python list
Python listPython list
Python list
 
Python PCEP Lists Collections of Data
Python PCEP Lists Collections of DataPython PCEP Lists Collections of Data
Python PCEP Lists Collections of Data
 
List in Python
List in PythonList in Python
List in Python
 
Unit - 4.ppt
Unit - 4.pptUnit - 4.ppt
Unit - 4.ppt
 
Python programming Part -6
Python programming Part -6Python programming Part -6
Python programming Part -6
 
Python lists & sets
Python lists & setsPython lists & sets
Python lists & sets
 
RANDOMISATION-NUMERICAL METHODS FOR ENGINEERING.pptx
RANDOMISATION-NUMERICAL METHODS  FOR ENGINEERING.pptxRANDOMISATION-NUMERICAL METHODS  FOR ENGINEERING.pptx
RANDOMISATION-NUMERICAL METHODS FOR ENGINEERING.pptx
 
Python lists
Python listsPython lists
Python lists
 
Lecture2.pptx
Lecture2.pptxLecture2.pptx
Lecture2.pptx
 
Csharp_List
Csharp_ListCsharp_List
Csharp_List
 
List Data Structure.docx
List Data Structure.docxList Data Structure.docx
List Data Structure.docx
 
python_avw - Unit-03.pdf
python_avw - Unit-03.pdfpython_avw - Unit-03.pdf
python_avw - Unit-03.pdf
 

More from deepalishinkar1

Operators in python
Operators in pythonOperators in python
Operators in pythondeepalishinkar1
 
Data handling in python
Data handling in pythonData handling in python
Data handling in pythondeepalishinkar1
 
Practical approach on numbers system and math module
Practical approach on numbers system and math modulePractical approach on numbers system and math module
Practical approach on numbers system and math moduledeepalishinkar1
 
Demonstration on keyword
Demonstration on keywordDemonstration on keyword
Demonstration on keyworddeepalishinkar1
 
Numbers and math module
Numbers and math moduleNumbers and math module
Numbers and math moduledeepalishinkar1
 
Basic concepts of python
Basic concepts of pythonBasic concepts of python
Basic concepts of pythondeepalishinkar1
 
Introduction to python
Introduction to python Introduction to python
Introduction to python deepalishinkar1
 

More from deepalishinkar1 (7)

Operators in python
Operators in pythonOperators in python
Operators in python
 
Data handling in python
Data handling in pythonData handling in python
Data handling in python
 
Practical approach on numbers system and math module
Practical approach on numbers system and math modulePractical approach on numbers system and math module
Practical approach on numbers system and math module
 
Demonstration on keyword
Demonstration on keywordDemonstration on keyword
Demonstration on keyword
 
Numbers and math module
Numbers and math moduleNumbers and math module
Numbers and math module
 
Basic concepts of python
Basic concepts of pythonBasic concepts of python
Basic concepts of python
 
Introduction to python
Introduction to python Introduction to python
Introduction to python
 

Recently uploaded

Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 

Recently uploaded (20)

Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 

Data type list_methods_in_python

  • 2. Creating List • Lists in Python can be created by just placing the sequence inside the square brackets[ ]. • Creating list: • Initial black list • List with the use of String • Use of Multiple Values • Multi-dimesional list 4/3/2020LIST IN PYTHON 2
  • 3. List Index â–Ş We can use the index operator [] to access an item in a list. Index starts from 0. So, a list having 5 elements will have index from 0 to 4. â–Ş Nested list are accessed using nested indexing. â–Ş 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. 4/3/2020LIST IN PYTHON 3
  • 4. List Method:Append • append( ) : ➢ It is built-in function ➢ Only one element at a time can be added to the list. ➢ For addition of multiple elements with append ( ) method loops are used. ➢ Tuples,Sets,Lists can also be added to existing list with the use of append( ) method. 4/3/2020LIST IN PYTHON 4
  • 5. List Method :Insert • insert( ) : ➢ It is built-in function ➢ The insert() method inserts an element to the list at a given index. ➢ The syntax of insert() method is: list.insert(index,element) ➢ The insert() function takes two parameters: • index - position where an element needs to be inserted • element - this is the element to be inserted in the list • This method does not return anything.Return none( ). 4/3/2020LIST IN PYTHON 5
  • 6. List Method :Extend • extend ( ) : ➢ It is built-in function ➢ The extend() extends the list by adding all items of a list (passed as an argument) to the end. ➢ The syntax of insert() method is: list1.extend(list2) • If you need to add elements of other native datatypes (like tuple and set) to the list. • E.g.list1.extend(list(tuple_type)) 4/3/2020LIST IN PYTHON 6
  • 7. Slicing List • We can access a range of items in a list by using the slicing operator (colon). my_list = ['c','o','r','o','n','a','2','0','2','0'] # elements 3rd to 5th print(my_list[2:5]) # elements beginning to 4th print(my_list[:-5]) # elements 6th to end print(my_list[5:]) # elements beginning to end print(my_list[:]) C O R O N A 2 0 2 0 0 1 2 3 4 5 6 7 8 9 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 Indexing Negative Indexing 4/3/2020LIST IN PYTHON 7
  • 8. Add element from List • List are mutable, meaning, their elements can be changed unlike string or tuple. • We can use assignment operator (=) to change an item or a range of items. # mistake values odd = [2, 4, 6, 8] # change the 1st item odd[0] = 1 # Output: [1, 4, 6, 8] print(odd) # change 2nd to 4th items odd[1:4] = [3, 5, 7] # Output: [1, 3, 5, 7] print(odd) 4/3/2020LIST IN PYTHON 8
  • 9. Delete element from List • We can delete one or more items from a list using the keyword del. It can even delete the list entirely. my_list = ['p','r','o','b','l','e','m'] # delete one item del my_list[2] print(my_list) # delete multiple items del my_list[1:5] print(my_list) # delete entire list del my_list print(my_list) 4/3/2020LIST IN PYTHON 9
  • 10. Delete slice of items from List • We can also delete items in a list by assigning an empty list to a slice of elements. my_list = ['p','r','o','b','l','e','m’] my_list[2:3] = [] print(my_list) my_list[2:5] = [] print(my_list) 4/3/2020LIST IN PYTHON 10
  • 11. Remove( ) item from List • We can use remove() method to remove the given item my_list = ['p','r','o','b','l','e','m'] my_list.remove('p') # Output: ['r', 'o', 'b', 'l', 'e', 'm'] print(my_list) 4/3/2020LIST IN PYTHON 11
  • 12. Pop( ) item from List • pop() method to remove an item at the given index. • The pop() method removes and returns the last item if index is not provided. • This helps us implement lists as stacks (first in, last out data structure). print(my_list.pop( )) print(my_list) # Output: ['p','o','b','l','e'] my_list = ['p','r','o','b','l','e','m’] print(my_list.pop(1)) print(my_list) # Output: ['p','o','b','l','e','m’] 4/3/2020LIST IN PYTHON 12
  • 13. Clear List • We can also use the clear() method to empty a list. my_list = ['p','r','o','b','l','e','m’] my_list.clear() #Output : [ ] 4/3/2020LIST IN PYTHON 13
  • 14. + and * operator • We can also use + operator to combine two lists. This is also called concatenation. • The * operator repeats a list for the given number of times. odd = [1, 3, 5] # Output: [1, 3, 5, 9, 7, 5] print(odd + [9, 7, 5]) #Output: ["re", "re", "re"] print(["re"] * 3) 4/3/2020LIST IN PYTHON 14
  • 15. Count ( ) • count() method returns count of how many times obj occurs in list. • Syntax : list_name.count(object) • my_list = [3, 8, 1, 6, 0, 8, 4] # Output: 2 print(my_list.count(8)) 4/3/2020LIST IN PYTHON 15
  • 16. Index ( ) • Index( ) method returns the index of the first matched item. • Syntax : list_name.index(object) my_list = [3, 8, 1, 6, 0, 8, 4] # Output: 1 print(my_list.index(8)) 4/3/2020LIST IN PYTHON 16
  • 17. Index ( ) • Returns the index of first occurrence. Start and End index are not necessary parameters. Syntax: • list.index(element[,start[,end]]) List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1] print(List.index(2,2)) # will check from index 2. #Output List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1] print(List.index(2,2,6)) # will check from index 2 to 5. #Output : 4 4/3/2020LIST IN PYTHON 17
  • 18. sort( ) • The sort() method sorts the elements of a given list in a specific order - Ascending or Descending. • The syntax of sort() method is: list.sort(key=..., reverse=...) • Alternatively, you can also use Python's in-built function sorted() for the same purpose. • sorted(list, key=..., reverse=...) • Note: Simplest difference between sort() and sorted() is: sort() doesn't return any value while, sorted() returns an iterable list. 4/3/2020LIST IN PYTHON 18
  • 19. sort( ) • The sort function can be used to sort the list in both ascending and descending order. • Syntax: • Listname.sort( ) • This will srt the given list in ascending order. • Syntax • list_name.sort(reverse=True) • This will sort the given list in descending order. 4/3/2020LIST IN PYTHON 19
  • 20. sort( ) • To sorts according to user’s choice. • It has two optional parameters: reverse – If true, the list is sorted in descending order key – function that serves as a key for the sort comparison • It returns a sorted list according to the passed parameter. 4/3/2020LIST IN PYTHON 20
  • 21. Reverse( ) • The reverse( ) method reverses the elements of a given list. • Syntax: listname.reverse( ) • The reverse ( ) function doesn’t take any argument. • Reverse a list using slicing operator. • E.g. Reverse_list=listname[::-1] • To access individual elements of a list in reverse order,to use reversed( ) method. 4/3/2020LIST IN PYTHON 21
  • 22. Copy • The copy( ) method returns a shallow copy of the list. • It doesn’t take any parameter. • Syntax : • New_list=listname.copy( ) • A list can be copied with = operator. • Shallow copy of a list using slicing. 4/3/2020LIST IN PYTHON 22
  • 23. SUM,LEN • It returns the sums up the numbers in the list. • Syntax: • sum(iterable,start) • Calculates total length of List. • Syntax: • len(list_name) 4/3/2020LIST IN PYTHON 23
  • 24. MIN,MAX,CMP • Calculates minimum of all the elements of List. • Syntax: • min(List) • Calculates maximum of all the elements of List. Syntax: • max(List) • Compares elements of two lists. • Syntax: • cmp(list1, list2) 4/3/2020LIST IN PYTHON 24
  • 25. LIST • list() takes sequence types and converts them to lists. This is used to convert a given tuple into list. • Syntax: • list(seq) 4/3/2020LIST IN PYTHON 25
  • 26. Summary • List is a data type of container in Data Structures, which is used to store data • Homogeneous (same) • Hetrogeneous(different) • List are ordered and have a definite count. • List is mutable meaning you can change their content without changing their identity or changeable(Item can be alter) 4/3/2020LIST IN PYTHON 26