PYTHON NOTES JEGAN K K
FIND INDEX IN RANGES : (LIST)
To find the index of the value mentioned in the list if there are duplicates, we can use the
start and ending parameters.
Syntax ⇒ list_name.index(val, start, end)
SORTING OF LIST:
We can sort a list by ascending (or) descending order using the sort() function.
Syntax ⇒ list_name.sort()
list_name.sort(reversed = True)
Sorting of a list can be done using specific position values if it is a list of sub-lists. It can
be done using a combination of lambda() and sorted() function.
Syntax ⇒
list_name = sorted (list_name, key = fun(list))
def fun(list_name):
return lambda x : x [0]
fun() ⇒ defines the lambda function to take the first value of each sub-list
ADDING VALUES TO LIST:
We can add new values to list using the append() method.
Syntax ⇒ list_name.append(new_val)
To add multiple values at one go, it can be done using extend() method with all the values
combined into a list ‘[]’, passed as a parameter.
Syntax ⇒ list_name.extend([val1, val2])
POP() FUNCTION:
Pop function is used to pop the last index and the pop() used another time pops the
second last element respectively.
Syntax ⇒ list_name.pop()
To pop a specific index from list,
Syntax ⇒ list_name.pop(index)
FIND INDEX IN RANGES : (LIST)
To find the index of the value mentioned in the list if there are duplicates, we can use the
start and ending parameters.
Syntax ⇒ list_name.index(val, start, end)
SORTING OF LIST:
We can sort a list by ascending (or) descending order using the sort() function.
Syntax ⇒ list_name.sort()
list_name.sort(reversed = True)
Sorting of a list can be done using specific position values if it is a list of sub-lists. It can
be done using a combination of lambda() and sorted() function.
Syntax ⇒
list_name = sorted (list_name, key = fun(list))
def fun(list_name):
return lambda x : x [0]
fun() ⇒ defines the lambda function to take the first value of each sub-list
ADDING VALUES TO LIST:
We can add new values to list using the append() method.
Syntax ⇒ list_name.append(new_val)
To add multiple values at one go, it can be done using extend() method with all the values
combined into a list ‘[]’, passed as a parameter.
Syntax ⇒ list_name.extend([val1, val2])
POP() FUNCTION:
Pop function is used to pop the last index and the pop() used another time pops the
second last element respectively.
Syntax ⇒ list_name.pop()
To pop a specific index from list,
Syntax ⇒ list_name.pop(index)