SlideShare a Scribd company logo
1 of 16
Creating and Accessing Lists
Lists are mutable, you can change elements of a list in place.
In other words, the memory address of a list will not change
even after you change its values.
Creating Lists
To create a list, put a number of expressions in square
brackets and separate the items by commas. For example
[2, 4, 6]
['abc, 'def']
[1, 2.8, 3, 4.0]
Consider some more examples
•The empty list
The empty list is [ ]. You can also create an empty list as:
L = List()
It will generate an empty list and name that list as L.
•Long lists
If a list contains many elements, then to enter such long lists,
you can split it across several lines, like below:
sqrs = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196,
225, 256, 289, 324, 361, 408, 441, 484, 529, 576, 625]
•Nested lists
A list can have an element in it, which itself is a list. Such a list is
called nested list, e.g.
L1=[3, 4, [5, 6], 7]
>>> L1[2][0]
5
>>>L1[2][1]
6
The eval() function
The eval() function of Python can be used to evaluate and
return the result of an expression given as string. For
example
>>> y= eval("3*10")
>>>print (y)
30
>>>var1=eval (input ("Enter Value: "))
Enter value: 15+ 3
>>>print (var1, type(var1) )
18 <class 'int'>
>>>var1= eval (input ("Enter Value: "))
Enter value: 89.9
>>>print(var1, type (var1))
89.9 <class 'float'>
Comparing Lists
Comparison Result
[1, 2, 8, 9] < [9, 1] True
[1, 2, 8, 9] < [1, 2, 9, 1] True
[1, 2, 8, 9] < [1, 2, 8, 4] False
List Operations
Joining Lists
Joining two lists is very easy just like you perform addition.
>>> lst1 =[10, 12, 14]
>>> lst2 = [20, 22, 24]
>>> lst3 = [30, 32, 34]
>>>lst = lst1 + lst2 + lst3
>>>lst
[10, 12, 14, 20, 22, 24, 30, 32, 34]
The + operator when used with lists requires that both the operands
must be of list types. For example, following expression will result into
error:
list + number
list + complex-number
list + string
Repeating Lists
You can use * operator to replicate a list specified number of times,
e.g.:
>>>Ist1 = [1, 3, 5]
>>>lst1*3
[1, 3, 5, 1, 3, 5, 1, 3, 5]
Slicing the Lists
Use indexes of list elements to create list slices as per
following format:
seq = L[start : stop]
>>> Ist =[10, 12, 14, 20, 22, 24, 30, 32, 34]
>>>seq= lst [ 3: -3]
>>>seq
[20, 22, 24]
>>>L1=[2,3,4,5,6,7,8]
>>>L1[2:5]
[4,5,6]
>>>L1[6:10]
[8]
>>>L1[10:20]
[ ]
List Operations
Cont…
seq=L[start:stop:step]
>>>L1=[2,3,4,5,6,7,8,9,10]
>>>L1[0:6:2]
[2,4,6]
>>>L1[2:8:3]
[4,7]
>>>L4[5::2]
[7,9]
List Operations
List functions and methods
1. The index method
Syntax:-
List. index (<item>)
For example,
>>> L1 = [13, 18, 11, 16, 18, 14]
>>> L1.index (18)
1
2. The append method
Syntax:-
List. append (<item>)
>>>colours = [ ‘red’, ‘green’, ‘blue’]
>>>colours . append (‘yellow’)
>>>colours
[‘red’, ‘green’, ‘blue’, ‘yellow’]
3. The extend method
List.extend (<list>)
>>>t1= ['a', 'b', 'c']
>>>t2 = ['d, 'e']
>>>t1.extend(t2)
>>> t1
['a', 'b', 'c', 'd', 'e']
>>>t2
['d','e']
4. The insert method
List.insert(<pos>,<item>)
>>> t1 = ['a', 'e', 'u']
>>>t1.insert (2, 'i')
>>> t1
['a','e','i','u']
List functions and methods
5. The pop method
The pop() is used to remove the item from the list. lt is used as per following syntax:
List.pop(<index>)
>>>t1
['k', 'a', 'e', 'i', 'p', 'q', 'u']
>>> ele1= t1.pop(0)
>>>ele1
'k'
>>> t1
['a', 'e', 'i', 'p', 'q, 'u']
>> ele2 =t1.pop()
>>> ele2
'u'
>>>t1
['a', 'e', 'i', 'p', 'q']
6. The remove method
The remove( ) method removes the first occurrence of given item from the list. It is used as per
following format:
List.remove ( <values> )
>>>t1= ['a', 'e', 'i', 'p', 'q', 'a', 'q', 'p']
>>>t1.remove ('a')
>>> t1
[‘e’, 'i', 'p', 'q', 'a', 'q', 'p']
List functions and methods
7. The clear method
List.clear()
>>>L1 = [2, 3, 4, 5]
>> >L1.clear()
>>>L1
[ ]
8. The count method
This function returns the count or the item that you passed as argument.
If the given item is not in the list, it returns zero, it is used as per following
format:
List.count ( <item> )
L1 = [13, 18, 20, 10, 18, 23]
>>> L1.count[18]
2
>>>L1.count [28]
0
List functions and methods
9. The reverse method
The reverse() reverses the items of the list. The syntax to use reverse
method is -
List.reverse()
>>> t1
['e', 'i', 'q', 'a', 'q, 'p']
>>>t1. reverse()
>>>t1
['p, 'q','a', 'q', 'i', 'e']
10. The sort method
The sort() function sorts the items of the list, by default in increasing
order. This is done "in place", i.e., it does not create a new list. It is used
as per following syntax:
List.sort ()
>>> t1=['e','i', 'q', 'a', 'q', 'p']
>>> t1.sort()
>>> t1
['a','e', 'i', 'p', 'q', 'q']
List functions and methods
Program
 Program to print elements of a list [ 'q', 'w', 'e', 'r', 't', 'y'] in
separate lines along with element's both indexes (positive
and negative).
L= [ 'q','w', 'e', 'r', 't','y']
length=len(L)
for a in range (length) :
print ("At indexes", a, "and", (a - length), "element:", L[a])
Sample run of above program is:
At indexes 0 and -6 element : q
At indexes 1 and -5 element: w
At indexes 2 and-4 element: e
At indexes 3 and -3 element: r
At indexes 4 and -2 element: t
At indexes 5 and -1 element: y
Creating and Accessing Lists in Python

More Related Content

What's hot (20)

ARRAY
ARRAYARRAY
ARRAY
 
Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms Arrays
 
Pytho_tuples
Pytho_tuplesPytho_tuples
Pytho_tuples
 
Array
ArrayArray
Array
 
Row major and column major in 2 d
Row major and column major in 2 dRow major and column major in 2 d
Row major and column major in 2 d
 
LIST IN PYTHON
LIST IN PYTHONLIST IN PYTHON
LIST IN PYTHON
 
Array
ArrayArray
Array
 
Data type list_methods_in_python
Data type list_methods_in_pythonData type list_methods_in_python
Data type list_methods_in_python
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
 
Pytho lists
Pytho listsPytho lists
Pytho lists
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
 
arrays
arraysarrays
arrays
 
Data structure ppt
Data structure pptData structure ppt
Data structure ppt
 
9 python data structure-2
9 python data structure-29 python data structure-2
9 python data structure-2
 
Python tuples and Dictionary
Python   tuples and DictionaryPython   tuples and Dictionary
Python tuples and Dictionary
 
Presentation on array
Presentation on array Presentation on array
Presentation on array
 
Set data structure
Set data structure Set data structure
Set data structure
 
Lecture 5 data structures and algorithms
Lecture 5 data structures and algorithmsLecture 5 data structures and algorithms
Lecture 5 data structures and algorithms
 
02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory Mapping
 

Similar to Creating and Accessing Lists in Python

GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdfGE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdfAsst.prof M.Gokilavani
 
List_tuple_dictionary.pptx
List_tuple_dictionary.pptxList_tuple_dictionary.pptx
List_tuple_dictionary.pptxChandanVatsa2
 
python_avw - Unit-03.pdf
python_avw - Unit-03.pdfpython_avw - Unit-03.pdf
python_avw - Unit-03.pdfAshaWankar1
 
15CS664- Python Application Programming - Module 3
15CS664- Python Application Programming - Module 315CS664- Python Application Programming - Module 3
15CS664- Python Application Programming - Module 3Syed Mustafa
 
15CS664-Python Application Programming - Module 3 and 4
15CS664-Python Application Programming - Module 3 and 415CS664-Python Application Programming - Module 3 and 4
15CS664-Python Application Programming - Module 3 and 4Syed Mustafa
 
Lists.pptx
Lists.pptxLists.pptx
Lists.pptxYagna15
 
The Ring programming language version 1.5.3 book - Part 22 of 184
The Ring programming language version 1.5.3 book - Part 22 of 184The Ring programming language version 1.5.3 book - Part 22 of 184
The Ring programming language version 1.5.3 book - Part 22 of 184Mahmoud Samir Fayed
 
Python List.ppt
Python List.pptPython List.ppt
Python List.pptT PRIYA
 
Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxMihirDatir
 
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 210Mahmoud Samir Fayed
 
List Data Structure.docx
List Data Structure.docxList Data Structure.docx
List Data Structure.docxmanohar25689
 
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 202Mahmoud Samir Fayed
 
Lecture2.pptx
Lecture2.pptxLecture2.pptx
Lecture2.pptxSakith1
 
Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxMihirDatir1
 

Similar to Creating and Accessing Lists in Python (20)

Module III.pdf
Module III.pdfModule III.pdf
Module III.pdf
 
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdfGE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
 
List_tuple_dictionary.pptx
List_tuple_dictionary.pptxList_tuple_dictionary.pptx
List_tuple_dictionary.pptx
 
python_avw - Unit-03.pdf
python_avw - Unit-03.pdfpython_avw - Unit-03.pdf
python_avw - Unit-03.pdf
 
15CS664- Python Application Programming - Module 3
15CS664- Python Application Programming - Module 315CS664- Python Application Programming - Module 3
15CS664- Python Application Programming - Module 3
 
15CS664-Python Application Programming - Module 3 and 4
15CS664-Python Application Programming - Module 3 and 415CS664-Python Application Programming - Module 3 and 4
15CS664-Python Application Programming - Module 3 and 4
 
Python for Beginners(v3)
Python for Beginners(v3)Python for Beginners(v3)
Python for Beginners(v3)
 
Lists.pptx
Lists.pptxLists.pptx
Lists.pptx
 
Unit - 4.ppt
Unit - 4.pptUnit - 4.ppt
Unit - 4.ppt
 
The Ring programming language version 1.5.3 book - Part 22 of 184
The Ring programming language version 1.5.3 book - Part 22 of 184The Ring programming language version 1.5.3 book - Part 22 of 184
The Ring programming language version 1.5.3 book - Part 22 of 184
 
Python List.ppt
Python List.pptPython List.ppt
Python List.ppt
 
Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptx
 
Python lists &amp; sets
Python lists &amp; setsPython lists &amp; sets
Python lists &amp; sets
 
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
 
List Data Structure.docx
List Data Structure.docxList Data Structure.docx
List Data Structure.docx
 
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
 
Python lecture 04
Python lecture 04Python lecture 04
Python lecture 04
 
Chapter 15 Lists
Chapter 15 ListsChapter 15 Lists
Chapter 15 Lists
 
Lecture2.pptx
Lecture2.pptxLecture2.pptx
Lecture2.pptx
 
Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptx
 

More from nuripatidar

More from nuripatidar (9)

Python program
Python programPython program
Python program
 
Python operators
Python operatorsPython operators
Python operators
 
Tuple.py
Tuple.pyTuple.py
Tuple.py
 
Indian history
Indian historyIndian history
Indian history
 
Python basic Program
Python basic ProgramPython basic Program
Python basic Program
 
Python Programme list
Python Programme listPython Programme list
Python Programme list
 
Chemistry
ChemistryChemistry
Chemistry
 
Python statements
Python statementsPython statements
Python statements
 
Python data type
Python data typePython data type
Python data type
 

Recently uploaded

Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 

Recently uploaded (20)

Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
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
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 

Creating and Accessing Lists in Python

  • 1.
  • 2.
  • 3. Creating and Accessing Lists Lists are mutable, you can change elements of a list in place. In other words, the memory address of a list will not change even after you change its values. Creating Lists To create a list, put a number of expressions in square brackets and separate the items by commas. For example [2, 4, 6] ['abc, 'def'] [1, 2.8, 3, 4.0]
  • 4. Consider some more examples •The empty list The empty list is [ ]. You can also create an empty list as: L = List() It will generate an empty list and name that list as L. •Long lists If a list contains many elements, then to enter such long lists, you can split it across several lines, like below: sqrs = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 408, 441, 484, 529, 576, 625] •Nested lists A list can have an element in it, which itself is a list. Such a list is called nested list, e.g. L1=[3, 4, [5, 6], 7] >>> L1[2][0] 5 >>>L1[2][1] 6
  • 5. The eval() function The eval() function of Python can be used to evaluate and return the result of an expression given as string. For example >>> y= eval("3*10") >>>print (y) 30 >>>var1=eval (input ("Enter Value: ")) Enter value: 15+ 3 >>>print (var1, type(var1) ) 18 <class 'int'> >>>var1= eval (input ("Enter Value: ")) Enter value: 89.9 >>>print(var1, type (var1)) 89.9 <class 'float'>
  • 6. Comparing Lists Comparison Result [1, 2, 8, 9] < [9, 1] True [1, 2, 8, 9] < [1, 2, 9, 1] True [1, 2, 8, 9] < [1, 2, 8, 4] False
  • 7. List Operations Joining Lists Joining two lists is very easy just like you perform addition. >>> lst1 =[10, 12, 14] >>> lst2 = [20, 22, 24] >>> lst3 = [30, 32, 34] >>>lst = lst1 + lst2 + lst3 >>>lst [10, 12, 14, 20, 22, 24, 30, 32, 34] The + operator when used with lists requires that both the operands must be of list types. For example, following expression will result into error: list + number list + complex-number list + string Repeating Lists You can use * operator to replicate a list specified number of times, e.g.: >>>Ist1 = [1, 3, 5] >>>lst1*3 [1, 3, 5, 1, 3, 5, 1, 3, 5]
  • 8. Slicing the Lists Use indexes of list elements to create list slices as per following format: seq = L[start : stop] >>> Ist =[10, 12, 14, 20, 22, 24, 30, 32, 34] >>>seq= lst [ 3: -3] >>>seq [20, 22, 24] >>>L1=[2,3,4,5,6,7,8] >>>L1[2:5] [4,5,6] >>>L1[6:10] [8] >>>L1[10:20] [ ] List Operations
  • 10. List functions and methods 1. The index method Syntax:- List. index (<item>) For example, >>> L1 = [13, 18, 11, 16, 18, 14] >>> L1.index (18) 1 2. The append method Syntax:- List. append (<item>) >>>colours = [ ‘red’, ‘green’, ‘blue’] >>>colours . append (‘yellow’) >>>colours [‘red’, ‘green’, ‘blue’, ‘yellow’]
  • 11. 3. The extend method List.extend (<list>) >>>t1= ['a', 'b', 'c'] >>>t2 = ['d, 'e'] >>>t1.extend(t2) >>> t1 ['a', 'b', 'c', 'd', 'e'] >>>t2 ['d','e'] 4. The insert method List.insert(<pos>,<item>) >>> t1 = ['a', 'e', 'u'] >>>t1.insert (2, 'i') >>> t1 ['a','e','i','u'] List functions and methods
  • 12. 5. The pop method The pop() is used to remove the item from the list. lt is used as per following syntax: List.pop(<index>) >>>t1 ['k', 'a', 'e', 'i', 'p', 'q', 'u'] >>> ele1= t1.pop(0) >>>ele1 'k' >>> t1 ['a', 'e', 'i', 'p', 'q, 'u'] >> ele2 =t1.pop() >>> ele2 'u' >>>t1 ['a', 'e', 'i', 'p', 'q'] 6. The remove method The remove( ) method removes the first occurrence of given item from the list. It is used as per following format: List.remove ( <values> ) >>>t1= ['a', 'e', 'i', 'p', 'q', 'a', 'q', 'p'] >>>t1.remove ('a') >>> t1 [‘e’, 'i', 'p', 'q', 'a', 'q', 'p'] List functions and methods
  • 13. 7. The clear method List.clear() >>>L1 = [2, 3, 4, 5] >> >L1.clear() >>>L1 [ ] 8. The count method This function returns the count or the item that you passed as argument. If the given item is not in the list, it returns zero, it is used as per following format: List.count ( <item> ) L1 = [13, 18, 20, 10, 18, 23] >>> L1.count[18] 2 >>>L1.count [28] 0 List functions and methods
  • 14. 9. The reverse method The reverse() reverses the items of the list. The syntax to use reverse method is - List.reverse() >>> t1 ['e', 'i', 'q', 'a', 'q, 'p'] >>>t1. reverse() >>>t1 ['p, 'q','a', 'q', 'i', 'e'] 10. The sort method The sort() function sorts the items of the list, by default in increasing order. This is done "in place", i.e., it does not create a new list. It is used as per following syntax: List.sort () >>> t1=['e','i', 'q', 'a', 'q', 'p'] >>> t1.sort() >>> t1 ['a','e', 'i', 'p', 'q', 'q'] List functions and methods
  • 15. Program  Program to print elements of a list [ 'q', 'w', 'e', 'r', 't', 'y'] in separate lines along with element's both indexes (positive and negative). L= [ 'q','w', 'e', 'r', 't','y'] length=len(L) for a in range (length) : print ("At indexes", a, "and", (a - length), "element:", L[a]) Sample run of above program is: At indexes 0 and -6 element : q At indexes 1 and -5 element: w At indexes 2 and-4 element: e At indexes 3 and -3 element: r At indexes 4 and -2 element: t At indexes 5 and -1 element: y