MMT List Py
MMT List Py
# Print areas
SN Function with Description
1 len(list)
Gives the total length of the list.
2 max(list)
Returns item from the list with max value.
3 min(list)
Returns item from the list with min value.
4. sum(list)
Returns the sum of all elements of list
TwoList can be combined using +
operator
.
mylist = []
mylist.append(5)
mylist.append(8)
mylist.appe nd(12)
#prints
[5,8,12]
print(mylist)
insert(index,
element)
Insert the elements in sorted order.
Algorithm
If list is empty, append element to list.
If element is less than first element, insert front.
Traverse till current element is less than
element to insert or till end of list
Insert the element.
We can add a series of elements using
extend method or + operator.
a= [1,2,3]
b = [4,5,6]
Understand the difference
between
a.append(b) and
a.extend(b)
To delete last
element
To delete element at specific
Index
Remove method takes the key element
to delete and removes it if present.
Inall the previous methods, we were not
able to determine the index where the key
element is present.
index() method finds the given element in
a list and returns its position.
If the same element is present more than
once, index() method returns its
smallest/first position.
If not found, it raises
a ValueError exception indicating the
element is not in the list.
The index method finds the first occurrence of a list item
and returns its index.
If the item isn't in the list, it raises a ValueError.
m y l i s t [ s t a r t I n d e x : endIndex]
Mylist[startIndex : endIndex : step]
m y l i s t = [ 5 , 8 , 12 , 20 , 25, 50]
p r i n t ( ' F u l l l i s t elements' , mylist)
print('printing a l t e r n a t e elements', m y l i s t [ : :
2])
kitchen
BMI = weight/height2
print (bmi)
Python List
Convert a list of temperature values
from celsius to fahrenheit
Given a list of elements, create a new list which
has elements with one added to every element
of old list.
nums = [12, 8, 21, 3, 16]
new_nums = [13, 9, 22, 4, 17]
Collapse for loops for building lists into a
single line
Components
Iterable
Iterator variable
Output expression
Given
a list of elements extract all the
odd elements and place in new list.
Conditionals on the iterable
[num ** 2 for num in range(10) if num % 2 == 0]