Python Tricks for beginners
Python List Small Tricks For Beginners
I hope you knows about slicing, + operator, indexing, for loop. Here i show you how to use small function for your benefits when interviwers ask to do not use any built-in function.
Let’s Jump to `+` operator, see how this operator works on list.
Python3
# code
list1 = [1, 2]
list2 = [3, 4]
print(list1 + list2)
Output
[1, 2, 3, 4]
1. How to create own len() function for Python List?
Algorithms
- create counted_length variable and store 0
- iterate through list for size of list times
- Update counted_length by adding 1 in them, when for loop iterate
- Now return the counted_length
Python3
# code
def length(array):
counted_length = 0
for i in array:
counted_length += 1
return counted_length#Driver/Execution code for Testingc = [1, 2, 3, 4, 5, 6]
print(length(c))# checking for mixed data types
d = ['1', '2', 'a', 4.5, -5, "6", [1, 2], (1, 2), {'key':'value', 'key2': 2}, {1, 2, 3}]
print(length(d))
Output
6
10
2. How to create own append Function for Python List?
Algorithm :-
- First create empty list
- Iterate over an array of list, for size of array time.
- Add every iteration of array in previous array.
- If Size of new_array reached equal to array then add item. Break the loop
Python3
def appending(array, item):
lst = []
for i in range(len(array)):
lst = lst + [array[i]]
if len(lst) == len(array):
lst = lst + [item]
return lst
# driver/execution code
x = [1, 2, 3, 4, 5, 6]
print(appending(x, 6))
Output
[1, 2, 3, 4, 5, 6, 6]
Another Method
Python3
def appending(array, item):
return array + [item]# driver/explanation codeh = [1, 2, 3, 4, 5, 6]
print(appending(h, 6))
Output
[1, 2, 3, 4, 5, 6, 6]
Simple and sweeter code. Above function takes two argument array takes as a list and item for adding in list. Here array is an list and item is an integer, so we have change it into list then we’ll use ‘+’ to add in array. I hope you can understand.
3. How to create custom insert() function for python list?
Algorithms:-
- As usual we created an empty list
- Now iterating through list of length of array times
- checking when pointer `i` is equal to index then add item and also add item of array that present at same index in list of array
- if pointer not equal to index else add index of array to new list named `lst`
- return lst
Python3
def inserting(array, index, item):
lst = []
for i in range(len(array)):
if i == index:
lst = lst + [item]
lst = lst + [array[i]]
else:
lst = lst + [array[i]] return lst# driver/execution code
y = [1, 2, 4, 5]
print(inserting(y, 2, 3))
Output
[1, 2, 3, 4, 5]
4. How to create Custom/Own remove() function for Python List?
Algorithms:-
- Iterate through list
- Add that item when pointer i in not equal to item.
Python3
def removing(array, item):
lst = []
for i in range(len(array)):
if array[i] != item:
lst = lst + [array[i]]
return lst
# driver/ececution code
z = [1, 2, 3, 4]
print(removing(z, 4))
Output
[1, 2, 3]
5. How to create Custom reverse() function for Python List?
Algorithms:
- iterating through list of array
- Adding one item from last
- Returning new list
Python3
def reverse(array):
lst = []
for i in range(len(array)):
lst = lst + [array[len(array)-1-i]]
return lst# driver/execution code
p = [1, 2, 3]
print(reverse(p))
Output
[3, 2, 1]
6. How to create Custom poping() function for Python List?
Algorithm:-
- Iterate len of array time
- Skip last iteration to pop last item
- Add every item in a new list
- return list
Python3
def poping(array):
lst = []
for i in range(len(array)-1):
lst = lst + [array[i]]
return lst
# driver/execution code
x = [1, 2, 3, 4, 5, 6]
print(poping(x))
Output
[1, 2, 3, 4, 5]
7. How to get min Value from list without using min() built-in function?
algorithm:-
- Sort in ascending
- Access 0th item of sorted array
Python3
def min(array):
for i in range(len(array)):
for j in range(0, len(array)-1-i):
if array[j]>array[j+1]:
array[j], array[j+1] = array[j+1], array[j]
return array[0]# driver/explanation code
x = [2, -2, 8, -8, 3, 7, 9]
print(min(x))
Output
-8
8. How to get max Value from list without using max() built-in function?
Algorithm:-
- Sort in ascending
- Access Last item of sorted array
Python3
def max(array):
for i in range(len(array)):
for j in range(0, len(array)-1-i):
if array[j]>array[j+1]:
array[j], array[j+1] = array[j+1], array[j]
return array[-1]# driver/explanation code
x = [2, -2, 8, -8, 3, 7, 9]
print(max(x))
Output
9
Explanation:-
Read full article here, this article is part of Python3 Tricks for begineers Series.
You can visit my website Here