0% found this document useful (0 votes)
8 views15 pages

Python Lists: Creation, Access, and Methods

The document provides an overview of Python lists, detailing their characteristics, creation methods, and basic operations such as accessing, modifying, and removing elements. It includes examples of list operations, iteration, and list comprehension. Additionally, it presents various programming problems related to lists for practice.

Uploaded by

mahendravusa9058
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)
8 views15 pages

Python Lists: Creation, Access, and Methods

The document provides an overview of Python lists, detailing their characteristics, creation methods, and basic operations such as accessing, modifying, and removing elements. It includes examples of list operations, iteration, and list comprehension. Additionally, it presents various programming problems related to lists for practice.

Uploaded by

mahendravusa9058
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

Python Programming

Lists

Assistant Professor & Technical Trainer


Madanapalle Institute of Technology & Science
sivakumarm@[Link]
9500600868
Lists
 A Python list is a data structure that stores a collection of values or objects in a specified
order (or sequence)

 A list is used to store the sequence of different types of data

x = [10,20, "mits",2.5,“JNTU",50,60] Example


[1, 2, 3, 4] == [4, 1, 3, 2]
Important Characteristics False
 Mutable
 Ordered
 Heterogeneous
 Allow Duplicates

2
Lists
How to create a Python List?
 A Python list is created by adding elements in the square brackets [ ].
Ex-1 Ex-2 Ex-3
Lsst = [1,2,3,4,5,8,5,4] Lsst = [’cse’,’cst’,’mca’,’ece’] Lsst = [1, 3.5, ’a’, ’mits’]
print(Lsst) print(Lsst) print(Lsst)

Ex-4 Ex-5
Lsst = [0] * 10 Lsst = [ ]
print(Lsst) print(Lsst)

 Creating a list within a list Ex-1


Lst1 = [1,2,3,4]
Lst2 = [5,6,7]
List3=[Lst1,Lst2]
3
Lists
Methods – Popular

4
Lists
How to access a Python List?
 Use list index [+ve value for forward] and negative index [for reverse]

Example
A[0] # prints a -> 10
A[len(A)] # Error
A[-1] # prints 50
A[-5] # prints 10
Example
A[2:5] # prints 30 40 50
A[-5:-2] # 10 20 30 A[2:] # prints 30 40 50
A[-5:-2]==A[0:3] # prints True A[:5] # 10 20 30 40 50
A[0:5:2] # prints 10 30 50
A[4:0:-2] # prints 50 30
A[:] # prints 10 20 30 40 50
A[::-1] # 50 40 30 20 10
5
Lists
Basic Operations
Operations Description
Length Total elements Ex: len([1,2,3,4,5]) Ans: 5
Concatenation Join two lists Ex: [1,2,3] + [4,5,6] Ans: [1,2,3,4,5,6]
Repetition Repeats elements Ex: [1,2,3]*2 Ans:[1,2,3,1,2,3]
in Checks presence Ex: 5 in [1,2,3,4,5] Ans: True
not in Checks absence Ex: 5 not in [1,2,3,4,6] Ans: True
Max Maximum value Ex: max([2,3,4,1]) Ans: 4
Min Minimum value Ex: min([2,3,4,1]) Ans: 1
Sum Adds the values Ex: sum([2,3,4,1]) Ans: 10
List Converts to list Ex: list1=list(‘1234’) Ans:[‘1’,’2’,’3’,’4’]
Sorted Sorted list Ex: list1=sorted([3,1,2,4]) Ans: [1,2,3,4]

6
Lists

List Assignment using ‘=‘ operator


 One list can be assignment to another list either completely or partially
 Both list will points to same memory .i.e alias
 Changes made with one alias affect the other

Example Example
Input1 = [1,2,3,4,5] Input1 = [1,2,3,4,5]
Input2 = Input1 Input2 = Input1[2:]
print(Input2) print(Input2)
Input2[0] = 10 Input2[0] = 10
print(Input1) print(Input1)

7
List

Iterating a List
 The objects in the list can be iterated over one by one, by using a for a loop
Program Program
mylist = [5, 8, ‘JNTU', 7.50, ‘mits'] mylist = [5, 8, ‘JNTU', 7.50, ‘mits']
for item in mylist: for i in range(0,len(mylist)):
print(item) print(mylist[i],end=‘ ‘)

 WAP to print only even numbers in the input list


 WAP to print the ASCII value of every character in a list
 WAP to swap only the even numbers in a list
 WAP to convert the input number into list Ex: 1562 -> [1,5,6,2]
8
List

Insertion in list
 New elements can be added depending on the position or at last
Program Program
mylist = [1,5,10,15,20] mylist = [1,5,10,15,20]
[Link](25) [Link](1,2)
print(mylist) print(mylist)
[Link]([30,35]) [Link](2,[6,7])
print(mylist)
Program
mylist = [1,5,10,15,20]
[Link]([25,30])
print(mylist)
9
List

Modification in list
 Modification can be done based on individual item or certain group
Example-1 Example-2
#Modifiying an individual items #Modifiying all items
mylist = list([2, 4, 6, 8, 10, 12]) mylist = list([2, 4, 6, 8])
mylist[0] = 20 for i in range(len(mylist)):
print(mylist) square = mylist[i] * mylist[i]
mylist[1:4] = [40, 60, 80] mylist[i] = square
print(mylist) print(mylist)
mylist[3:] = [80, 100, 120]
print(mylist)

10
List
Removal of element in list
 The elements from the list can be removed using the following list methods :
remove( ), pop( ), clear( ), del listname
Example-2
Example-1 #Removal of items using position
mylist = list([2, 4, 6, 8, 10, 12]) mylist = list([2, 4, 6, 8, 10, 12])
[Link](6) [Link](2) # 2nd index
mylist = list([6, 4, 6, 6, 8, 12])
print(mylist)
for item in mylist:
[Link]() # last value
[Link](6) #remove all
print(mylist)
print(mylist)
Example-3 Example-4
mylist = list([2, 4, 6, 8, 10, 12]) mylist = list([2, 4, 6, 8, 10, 12])
del mylist[2:5] [Link]()
mylist = list([6, 4, 6, 6, 8, 12]) print(mylist)
del mylist[3:] del mylist
11
Problems - Numbers
1. Program to find smallest and largest number in an array
2. Program to find missing number in an array of shuffled order
3. Program to find second largest number in an array
4. Program to print the re-arranged array by small – largest combination
5. Program to print Max contiguous Sequence
6. Program to print the most repeated number in an array
7. Program to print all unique elements in an array
8. Program to rotate an array by ‘N’ positions
9. Program to remove all duplicates in an array

12
Nearest Next Smaller number

13
Max Contiguous Sequence

14
List Comprehension
 Comprehension is a way of building a code block for defining, calling, and performing
operations on a series of values/ data elements
 There are 3 components of list comprehension, these are:
 Output expression: This one is optional and can be ignored.
 Iterable
 A variable representing members of the iterable is called Iterator Variable.

Example Example
numbers = [2, 5, 3, 7] numbers_list = [x for x in range(100) if x % 2 ==
square = [n**2 for n in numbers] 0 if x % 5 == 0]
print(square) print(numbers_list)

15

You might also like