[go: up one dir, main page]

0% found this document useful (0 votes)
5 views9 pages

List Chapter

Uploaded by

roshniguptaansh0
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)
5 views9 pages

List Chapter

Uploaded by

roshniguptaansh0
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/ 9

Chapter - List

 List is an ordered set(sequences) of characters(of any data type) within


square bracket separated by comma.
 It is mutable in nature.
 it is heterogeneous in nature means it can contain mix data types.

Creating and initializing list:

There are 3 methods to create a list.

Method 1:

We can create a list by using some values within the square bracket separated
by comma.

Example: list1=[1,"ram",10.5]

Method 2:

We can create an empty list by blank square bracket.

Example: Elist=[ ]

Note some facts:

A=B= [] # Both A and B will point to the same list


>>> A=B= []
>>> id (A)
8432296
>>> id (B)
8432296

A= []
B=A # Both A and B will point to the same list
>>> A= []
>>> B=A
>>> id (A)
47612312
>>> id (B)
47612312
A= []
B= [] # Both A and B are independent
>>> A= [];B= []
>>> id (A)
8432896
>>> id (B)
47580064

Accessing List:

We can access the list by using the name of the list.


Example:
str1= [2,3,7,8,4]
print (str1) #[2,3,7,8,4]

Accessing List Element:


We can access the list element by using the index (positive or negative) value
within the square bracket.
Syntax: lst_name[index]
Example:
>>> lst=[2,5,7,9,5,4,0]
>>> lst[4]
5
>>> lst[-3]
5
>>> lst[5%2]
5
If index value is out of range so it will create an error.
>>> str1=[3,4,5,6,7,8]
>>> str1[10]
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
str1[10]
IndexError: list index out of range

Traversing a List :
It means to go through with each element of a list sequentially.
Method 1 :
for i in list :
print(i)
Method 2 :
for i in range(len(list))
print(list[i])

#WAP to print an initialized list of friends.


#WAP to print a list in reverse order.

Lists are Mutable :


It means list is changeable or we can change its elements.
Example :
frds=['Anmol','Kiran','Vimal','Vidhya']
frds[0]='Ram'
frds[-1]='Sita'
print(frds) # ['Ram','Kiran','Vimal','Sita']

List Slices :
Slicing is the process of exacting a sublist of element(s) from the original list
and this sublist is called slice.
We can perform slicing by the use two indices in square brackets separated by
colon([]) and [m:n]
Example :

#Searching
lst=[2,3,4,5,67,8,9,0]
flag=0
elem=int(input("Enter element you want to search : "))
for i in lst :
if i == elem :
flag=1
break
if flag == 1 :
print("Seaching is successfully done !!!")
else :
print("Better Luck Next time :(")

Deleting List Element:

Method 1:
Using del statement:

It doesn't return any value.


Example :
>>> lst=[2,3,4,5,6,7,8,9]
>>> del lst[4]
>>> lst
[2, 3, 4, 5, 7, 8, 9]
>>> del lst[2:4]
>>> lst
[2, 3, 7, 8, 9]
>>> del lst
>>> lst
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
lst
NameError: name 'lst' is not defined

Method 2:
Using pop ():

It returns the deleted value.

>>> lst=[2,3,4,5,6,7,8,9]
>>> lst.pop()
9
>>> lst
[2, 3, 4, 5, 6, 7, 8]
>>> lst.pop(4)
6
>>> lst
[2, 3, 4, 5, 7, 8]

#WAp to delete allo the even numbers from a numeric list.

range() function :

>>> range(5)
range(0, 5)
>>> for i in range(5) :
print(i)

0
1
2
3
4
>>>
>>> for i in range(2,5) :
print(i,end =" ")
234
>>> for i in range(2,10,2) :
print(i,end =" ")

2468
>>> for i in range(2,10,-2) :
print(i,end =" ")

>>> for i in range(20,10,-2) :


print(i,end =" ")

20 18 16 14 12

List Operations:
1.) Concatenating Lists:
>>> l1=[2,4,6]
>>> l2=[1,3,5]
>>> l3=l1+l2
>>> l3
[2, 4, 6, 1, 3, 5]

Replicating a list:
>>> l1=[2,4,6]
>>> l1*4
[2, 4, 6, 2, 4, 6, 2, 4, 6, 2, 4, 6]

List membership:
1.) in:
>>> l1=[2,4,6]
>>> 2 in l1
True

2.) not in :
>>> 2 not in l1
False

List methods/functions :
Methods Description Example
len() >>> list1=[2,3,4,5,6,7,8]
>>> len(list1)
7
list() >>> str1="Python"
>>> list(str1)
['P', 'y', 't', 'h', 'o', 'n']
append() It adds the elements at the >>> list1=[2,34.4,6,"Ram"]
last of the list. >>> list1.append(50)
>>> list1
[2, 34.4, 6, 'Ram', 50]
extend() Appends each elements of the >>>list1=[2,34.4,6,"Ram"]
list passed as argument to the >>> list2=["sita","ravan"]
end of the given list. list1.extend(list2)
>>> list1
[2, 34.4, 6, 'Ram', 'sita',
'ravan']
insert() Inserts an element at a >>>list1.insert(2,"sita")
particular index in the list. >>> list1
[2, 34.4, 'sita', 6, 'Ram']
count() >>> list1=[2,4,5,6,52,2,2,2]
>>> list1.count(2)
4
index() >>> list1=[2,4,5,6,4,2,2,2]
>>> list1.index(4)
1
>>> list1.index(90)
list1.index(90)
ValueError: 90 is not in list
remove() >>>
list1=[2,4,5,6,72,3,4,52,2,2,2]
>>> list1.remove(5)
>>> list1
[2, 4, 6, 72, 3, 4, 52, 2, 2, 2]
max() >>> lst=[2,3,4,5,6,7,8,10.4]
>>> max(lst)
10.4
>>> lst=['ram','sita']
>>> max(lst)
'sita'
min|()
reverse() It reverses the list and doesn't >>> lst=[2,3,4,5,6,7,8,9]
returns any value. >>> lst1=lst.reverse()
>>> lst1
>>>
>>> lst
[9, 8, 7, 6, 5, 4, 3, 2]
sort() It sorts the list(by default in >>> lst=[3,4,9,2,8,4,0,1,5]
ascending order). >>> lst.sort()
To sort in descending order, >>> lst
make argument reverse=True. [0, 1, 2, 3, 4, 4, 5, 8, 9]
It doesn't return any value. >>> lst.sort(reverse=False)
>>> lst
[0, 1, 2, 3, 4, 4, 5, 8, 9]
>>> lst.sort(reverse=True)
>>> lst
[9, 8, 5, 4, 4, 3, 2, 1, 0]
>>>

#WAP to create a list of 5 numbers from user input and print the list.
#WAP to create a list LISTA containing positive and negative from the
console. Also, create two separate lists called PosNum and NegNum to store
positive and negative numbers respectively. Also, calculate and print the
total of all positive and negative numbers.
#WAP that checks a string is palindrome or not.
#Write a menu driven program to perform the following operations on a
list :
1. Add/append element into list
2. Delete elements from list
A- Delete at beginning
B-Delete at end
C- Delete at position
D-Delete by value
E- Exit
3. Show list elements
4. Quit
#Write an application in python, which repositions all the elements of the
list by shifting each of them one to one position before and by shifting the
first element to the last position.
Input :
0 1 2 3 4 5
22 25 70 32 12 40

Output :
0 1 2 3 4 5
25 70 32 12 40 22

#WAP to convert a date in form "mm/dd/yyyy" to "month day,year".


#WAP to delete all duplicate elements in a list.
Input :
[5,2,4,-5,12,2,7,4]
Output :
[-5,2,4,5,7,12]
#WAP to find all duplicate elements in a list .
Input :
[5,2,4,-5,12,12,2,7,-5,4]
Output :
[-5,2,4,12]

You might also like