Python Tricks for beginners

Sunil Kumar Shaw
4 min readApr 14, 2022

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

  1. create counted_length variable and store 0
  2. iterate through list for size of list times
  3. Update counted_length by adding 1 in them, when for loop iterate
  4. 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 :-

  1. First create empty list
  2. Iterate over an array of list, for size of array time.
  3. Add every iteration of array in previous array.
  4. 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:-

  1. As usual we created an empty list
  2. Now iterating through list of length of array times
  3. 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
  4. if pointer not equal to index else add index of array to new list named `lst`
  5. 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:-

  1. Iterate through list
  2. 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:

  1. iterating through list of array
  2. Adding one item from last
  3. 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:-

  1. Iterate len of array time
  2. Skip last iteration to pop last item
  3. Add every item in a new list
  4. 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:-

  1. Sort in ascending
  2. 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:-

  1. Sort in ascending
  2. 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:-

For explanation read here

Read full article here, this article is part of Python3 Tricks for begineers Series.

You can visit my website Here

--

--