0% found this document useful (0 votes)
2 views

Python Chapter7 List.pdf

The document provides an overview of Python lists, detailing their structure, operations, and built-in functions. It covers indexing, slicing, updating, deleting elements, and basic list operations such as concatenation and repetition. Additionally, it explains the use of functions like len(), max(), min(), and list() to manipulate and retrieve information from lists.

Uploaded by

adarshhalse45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Chapter7 List.pdf

The document provides an overview of Python lists, detailing their structure, operations, and built-in functions. It covers indexing, slicing, updating, deleting elements, and basic list operations such as concatenation and repetition. Additionally, it explains the use of functions like len(), max(), min(), and list() to manipulate and retrieve information from lists.

Uploaded by

adarshhalse45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Placement Python-Lists

Classes
YoganandSharma

The most basic data structure in Python is the


sequence. Each element of a sequence is assigned a
Training > Project >+91-9928016573

number -its position or index. The first index is zero,


y
Training by

the second index is one, and so forth.


d
JMD Study Computer

tu
Python has six built-in types of sequences, but the
most common ones are lists and tuples, which we
S
would see in this tutorial.
D
There are certain things you can do with all sequence

JM
types. These operations include indexing, slicing,
adding, multiplying, and checking for membership. In
addition, Python has built-in functions for finding the
length of a sequence and for finding its largest and
smallest elements.
Python Lists
Python Notes 1
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Lists
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
by Training.

The list is a most versatile datatype available in


+91-9928016573

Python which can be written as a list of comma-


Live Project

y
separated values (items) between square brackets.
Training

d
Important thing about a list is that items in a list need
u
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/

not be of the same type

St
Creating a list is as simple as putting different

D
comma-separated values between square brackets.
For example −
JM
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"]

Python Notes 2
Assistance Python-Lists:Accesing
TrainingClasses
YoganandSharma

Similar to string indices, list indices start at 0, and lists


+91-9928016573

can be sliced, concatenated and so on.

y
by

Accessing Values in Lists


d
JMD Study Computer
Live Project & 100%Job

To access values in lists, use the square brackets for


tu
slicing along with the index or indices to obtain value
S
available at that index. For example −

D
list1 = ['physics', 'chemistry', 1997, 2000];

JM
list2 = [1, 2, 3, 4, 5, 6, 7 ];
print ("list1[0]: ", list1[0])
print ("list2[1:5]: ", list2[1:5])

Python Notes 3
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/ Live Project
Training by Training.
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
+91-9928016573

output:
list1[0]: physics

J
list2[1:5]: [2, 3, 4, 5]

M
D
Study
Python-Lists: Accesing

Python Notes
4
Placement Python-Lists: updating
Classes
YoganandSharma

Updating Lists
Training > Project >+91-9928016573
You can update single or multiple elements of lists by

y
Training by

giving the slice on the left-hand side of the


d
JMD Study Computer

assignment operator, and you can add to elements in

tu
a list with the append() method. For example −
S
list = ['physics', 'chemistry', 1997, 2000];

D
print "Value available at index 2 : "
print list[2]
list[2] = 2001;JM
print "New value available at index 2 : "
print list[2]

Python Notes 5
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/ Live Project
Training by Training.
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
+91-9928016573

2001
1997
output:

JM
D
Value available at index 2 :

St
New value available at index 2 :
udy

Python Notes
Python-Lists: updating list

6
Assistance Python-Lists:Delete
TrainingClasses
YoganandSharma

Delete List Elements


+91-9928016573

To remove a list element, you can use either the del

y
by

statement if you know exactly which element(s) you


d
JMD Study Computer

are deleting or the remove() method if you do not


Live Project & 100%Job

know. For example −


tu
S
list1 = ['physics', 'chemistry', 1997, 2000];
print list1
D
del list1[2];
JM
print "After deleting value at index 2 : "
print list1

Python Notes 7
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/ Live Project
Training by Training.
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
+91-9928016573

output:

JM
D
S
['physics', 'chemistry', 2000].
t
After deleting value at index 2 :

u
Python-Lists:Delete
['physics', 'chemistry', 1997, 2000]

dy

Python Notes
8
Placement Python-Basic List Operations
Classes
YoganandSharma

Basic List Operations


Training > Project >+91-9928016573
Lists respond to the + and * operators much like

y
Training by

strings; they mean concatenation and repetition here


d
too, except that the result is a new list, not a string.
JMD Study Computer

tu
S
In fact, lists respond to all of the general sequence

D
operations we used on strings in the prior chapter.

JM

Python Notes 9
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/ Live Project
Training by Training.
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com YoganandSharma
+91-9928016573

['Hi!'] * 4

3 in [1, 2, 3]
len([1, 2, 3])

[1, 2, 3] + [4, 5, 6]
Python Expression

for x in [1, 2, 3]: print x,


J
3

True
Python-Lists

123
M
D
S
[1, 2, 3, 4, 5, 6]
Results

t
['Hi!', 'Hi!', 'Hi!', 'Hi!']
udy

Python Notes
Length

Iteration
Repetition

Membership
Concatenation
Description

10
Assistance Python-Lists
TrainingClasses
YoganandSharma

Indexing, Slicing, and Matrixes


+91-9928016573

Because lists are sequences, indexing and slicing work

y
by

the same way for lists as they do for strings.


d
JMD Study Computer
Live Project & 100%Job

Assuming following input −


L = ['spam', 'Spam', 'SPAM!']
tu
S
Python Expression
D Results Description

L[2]

L[-2]
JM
SPAM!

Spam
Offsets start at zero

Negative: count from


the right

L[1:] ['Spam', 'SPAM!'] Slicing fetches sections

Python Notes 11
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Lists
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
by Training.

Indexing, Slicing, and Matrixes


+91-9928016573

Because lists are sequences, indexing and slicing work


Live Project

y
the same way for lists as they do for strings.
Training

Assuming following input −


ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/

L = ['spam', 'Spam', 'SPAM!']


St
Python Expression
D Results Description

L[2]

L[-2]
JM
SPAM!

Spam
Offsets start at zero

Negative: count from


the right

L[1:] ['Spam', 'SPAM!'] Slicing fetches sections

Python Notes 12
Placement Python-function-len()
Classes
YoganandSharma

Description
Training > Project >+91-9928016573
The len()method returns the number of elements in

y
Training by

the list.
d
JMD Study Computer

Syntax
tu
S
Following is the syntax for len()method −
len(list) D
Parameters
JM
list−This is a list for which number of elements to
be counted.

Python Notes 13
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/ Live Project
Training by Training.
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com YoganandSharma
+91-9928016573

3
list.

Result
Example
Return Value

print (len(list1))

following result −
JM
D
Stud
Python-function-len()

list1 = ['physics', 'chemistry', 'maths']


y

Python Notes
When we run above program, it produces the
This method returns the number of elements in the

14
Assistance Python-function-max()
TrainingClasses
YoganandSharma

Description
+91-9928016573

The max()method returns the elements from the

y
by

list with maximum value.


d
JMD Study Computer
Live Project & 100%Job

Syntax
tu
S
Following is the syntax for max()method −
max(list) D
Parameters
JM
list−This is a list from which max valued element to
be returned.

Python Notes 15
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-function-max()
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma

Return Value
by Training.

+91-9928016573

This method returns the elements from the list with


Live Project

maximum value.
y
Training

Example
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/

St
list1, list2 = ['C++','Java', 'Python'], [456, 700, 200]
print ("Max value element : ", max(list1))
D
Result
JM
When we run above program, it produces the following
result −
Max value element : Python
Max value element : 700

Python Notes 16
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-function-min()
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma

Description
by Training.

The min()method returns the elements from the list


+91-9928016573

with minimum value.


Live Project

y
Training

Syntax
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/

min(list)
St
Following is the syntax for min()method −

Parameters D
JM
list−This is a list from which min valued element to be
returned.
Return Value
This method returns the elements from the list with
minimum value.

Python Notes 17
Placement Python-function-min()
Classes
YoganandSharma

Example
Training > Project >+91-9928016573
list1, list2 = ['C++','Java', 'Python'], [456, 700, 200]

y
Training by

print ("min value element : ", min(list1))


d
JMD Study Computer

tu
Result S
D
When we run above program, it produces the

J
following result −M
min value element : C++
min value element : 200

Python Notes 18
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-function-list()
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
by Training.

Description
+91-9928016573

The list()method takes sequence types and converts


Live Project

y
them to lists. This is used to convert a given tuple
Training

into list.
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/

Syntax
St
Following is the syntax for list()method −
list( seq) D
Parameters JM
seq−This is a tupleor string to be converted into
list.

Python Notes 19
Assistance Python-function-list()
TrainingClasses
YoganandSharma

Example
+91-9928016573

The following example shows the usage of list()

y
by

method.
d
JMD Study Computer
Live Project & 100%Job

aTuple= (123, 'C++', 'Java', 'Python')


list1 = list(aTuple)
tu
S
print ("List elements : ", list1)
D
str= "Hello World"

JM
list2 = list(str)
print ("List elements : ", list2)

Python Notes 20
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/ Live Project
Training by Training.
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
+91-9928016573

Result

following result −

JM
D
Stud
Python-function-list()

Python Notes
List elements : [123, 'C++', 'Java', 'Python']
When we run above program, it produces the

List elements : ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']

21
Placement Python-Methods-append()
Classes
YoganandSharma

Description
Training > Project >+91-9928016573
The append()method appends a passed objinto the

y
Training by

existing list.
d
JMD Study Computer

Syntax
tu
Following is the syntax for append()method −
list.append(obj) S
Parameters D
JM
obj−This is the object to be appended in the list.
Return Value
This method does not return any value but updates
existing list.

Python Notes 22
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Methods-append()
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma

Example
by Training.

+91-9928016573

The following example shows the usage of append()


Live Project

method.
y
Training

ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/

list1.append('C#')
St
list1 = ['C++', 'Java', 'Python']

D
print ("updated list : ", list1)

Result JM
When we run above program, it produces the
following result −
updated list : ['C++', 'Java', 'Python', 'C#']

Python Notes 23
Assistance Python-Methods-count()
TrainingClasses
YoganandSharma

Description
+91-9928016573

The count()method returns count of how many

y
by

times objoccurs in list.


d
JMD Study Computer
Live Project & 100%Job

Syntax
tu
Following is the syntax for count()method −
list.count(obj) S
Parameters D
JM
obj−This is the object to be counted in the list.
Return Value
This method returns count of how many times obj
occurs in list.

Python Notes 24
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Methods-count()
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma

Example
by Training.

+91-9928016573

The following example shows the usage of count()


Live Project

method.
y
Training

ud
aList= [123, 'xyz', 'zara', 'abc', 123];
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/

St
print ("Count for 123 : ", aList.count(123))
print ("Count for zara: ", aList.count('zara'))
D
Result
JM
When we run above program, it produces the following
result −
Count for 123 : 2
Count for zara: 1

Python Notes 25
Placement Python-Methods-extends()
Classes
YoganandSharma

Description
The extend()method appends the contents of seqto
Training > Project >+91-9928016573
list.
y
Training by

Syntax
d
JMD Study Computer

tu
Following is the syntax for extend()method −
list.extend(seq)
S
Parameters D
JM
seq−This is the list of elements

Return Value
This method does not return any value but add the
content to existing list.

Python Notes 26
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Methods-extends()
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma

Example
by Training.

+91-9928016573

The following example shows the usage of extend()


Live Project

method.
y
Training

ud
list1 = ['physics', 'chemistry', 'maths']
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/

list2 = list(range(5))
list1.extend(list2) St
D
print ('Extended List :', list1)

Result JM
When we run above program, it produces the following
result −
Extended List : ['physics', 'chemistry', 'maths', 0, 1, 2, 3, 4]

Python Notes 27
Assistance Python-Methods-index()
TrainingClasses
YoganandSharma

Description
The index()method returns the lowest index in list that
+91-9928016573

objappears.
y
by

d
JMD Study Computer

Syntax
Live Project & 100%Job

tu
Following is the syntax for index()method −
list.index(obj)
S
Parameters D
JM
obj−This is the object to be find out.

Return Value
This method returns index of the found object otherwise
raise an exception indicating that value does not find.

Python Notes 28
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Methods-index()
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma

Example
by Training.

The following example shows the usage of index()


+91-9928016573

method.
Live Project

y
Training

d
list1 = ['physics', 'chemistry', 'maths']
u
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/

St
print ('Index of chemistry', list1.index('chemistry'))
print ('Index of C#', list1.index('C#'))

Result D
JM
When we run above program, it produces the following
result −
Index of chemistry 1
Traceback(most recent call last): File "test.py", line 3, in
<module> print ('Index of C#', list1.index('C#'))
ValueError: 'C#' is not in list

Python Notes 29
Placement Python-Methods-insert()
Classes
YoganandSharma

Description
Training > Project >+91-9928016573
The insert()method inserts object objinto list at offset
index.
y
Training by

Syntax
d
JMD Study Computer

list.insert(index, obj) tu
Following is the syntax for insert()method −

Parameters S
D
index−This is the Index where the object objneed to
be inserted.
JM
obj−This is the Object to be inserted into the given list.
Return Value
This method does not return any value but it inserts the
given element at the given index.

Python Notes 30
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Methods-insert()
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma

Example
by Training.

+91-9928016573

The following example shows the usage of insert()


Live Project

method.
y
Training

ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/

St
list1 = ['physics', 'chemistry',
'maths'] list1.insert(1, 'Biology')

D
print ('Final list : ', list1)

Result JM
When we run above program, it produces the
following result −
Final list : ['physics', 'Biology', 'chemistry', 'maths']

Python Notes 31
Assistance Python-Methods-pop()
TrainingClasses
YoganandSharma

Description
+91-9928016573

The pop()method removes and returns last object or


objfrom the list.
y
by

Syntax
d
JMD Study Computer
Live Project & 100%Job

list.pop(obj= list[-1]) tu
Following is the syntax for pop()method −

S
Parameters D
JM
obj−This is an optional parameter, index of the object to
be removed from the list.

Return Value
This method returns the removed object from the list.

Python Notes 32
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Methods-pop()
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma

Example
by Training.

The following example shows the usage of pop() method.


+91-9928016573
Live Project

y
list1 = ['physics', 'Biology', 'chemistry', 'maths']
Training

list1.pop()
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/

print ("list now : ", list1)


list1.pop(1) St
D
print ("list now : ", list1)

Result
JM
When we run above program, it produces the following
result −
list now : ['physics', 'Biology', 'chemistry']
list now : ['physics', 'chemistry']

Python Notes 33
Placement Python-Methods-remove()
Classes
YoganandSharma

Parameters
Training > Project >+91-9928016573
obj−This is the object to be removed from the list.

y
Training by

d
JMD Study Computer

Return Value
tu
This method does not return any value but removes
S
the given object from the list.
D
JM

Python Notes 34
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Methods-remove()
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma

Example
by Training.

The following example shows the usage of remove()


+91-9928016573

method.
Live Project

y
Training

d
list1 = ['physics', 'Biology', 'chemistry', 'maths']
u
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/

list1.remove('Biology')
print ("list now : ", list1)
St
D
list1.remove('maths')
print ("list now : ", list1)

Result JM
When we run above program, it produces the following
result −
list now : ['physics', 'chemistry', 'maths']
list now : ['physics', 'chemistry']
Python Notes 35
Assistance Python-Methods-reverse()
TrainingClasses
YoganandSharma

Description
+91-9928016573

The reverse()method reverses objects of list in


place.
y
by

d
JMD Study Computer
Live Project & 100%Job

Syntax
tu
S
Following is the syntax for reverse()method −

D
list.reverse() Parameters
NA
JM
Return Value
This method does not return any value but reverse
the given object from the list.

Python Notes 36
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Methods-reverse()
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma

Example
by Training.

+91-9928016573

The following example shows the usage of reverse()


Live Project

method.
y
Training

ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/

'maths'] list1.reverse()
St
list1 = ['physics', 'Biology', 'chemistry',

D
print ("list now : ", list1)

Result
JM
When we run above program, it produces the
following result −
list now : ['maths', 'chemistry', 'Biology', 'physics']

Python Notes 37
Placement Python-Methods-sort()
Classes
YoganandSharma

Description
Training > Project >+91-9928016573
The sort()method sorts objects of list, use compare
function if given.
y
Training by

d
JMD Study Computer

Syntax
tu
S
Following is the syntax for sort()method −

D
list.sort([func]) Parameters
NA
JM
Return Value
This method does not return any value; it simply
sorts the contents of the given list.

Python Notes 38
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Methods-sort()
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
by Training.

Example
+91-9928016573

The following example shows the usage of sort()


Live Project

method.
y
Training

ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/

list1.sort() St
list1 = ['physics', 'Biology', 'chemistry', 'maths']

D
print ("list now : ", list1)
Result
JM
When we run above program, it produces the
following result −
list now : ['Biology', 'chemistry', 'maths', 'physics']

Python Notes 39

You might also like