![]() |
VOOZH | about |
In real world problems, we sometimes require to pad the element of list according to a condition that maximum characters have reached. Padding a number with 0 if it’s length is less than required by any field is one of the basic issues that occur in web forms in Web Development. Let’s discuss certain ways in which this issue can be solved.
Method #1: Using list comprehension This problem can be solved easily using the basic list comprehension in which we just need to use string formatting to perform the optional padding with 0 if size of each element is less than the specified size.
The original list is : [3, 54, 4, 100, 10] The list after K size element padding ['0003', '0054', '0004', '0100', '0010']
Time Complexity: O(n) where n is the number of elements in the in the list “test_list”. The list comprehension is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the in the list “test_list”.
Method #2: Using str.rjust() There is a function dedicated in python to do this job. The rjust function does the task of specifying the size of the string and also takes the character in which the character has to be padded.
The original list is : [3, 54, 4, 100, 10] The list after K size element padding ['0003', '0054', '0004', '0100', '0010']
Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Method #3: Using stringformatting
In this method, we use the f-string syntax to format each element of the original list with leading zeros using the :0{k} specifier. The 0 indicates that we want to pad with zeros, and k is the width of the padded string. Finally, we use a list comprehension to apply this formatting to each element of the original list and create the padded list.
['0003', '0054', '0004', '0100', '0010']
Time complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using numpy library:
Algorithm:
1.Import the numpy library.
2.Define the original list and K value.
3.Convert the original list to a numpy array of strings using the astype() method.
4.Use the np.char.zfill() function to pad each string in the array with zeros to a length of K.
5.Convert the padded numpy array back to a list using the tolist() method.
6.Print the original list and the padded list for comparison.
Output:
Original List: [3, 54, 4, 100, 10] Padded List: ['0003', '0054', '0004', '0100', '0010']
Time complexity:
The conversion of the original list to a numpy array takes O(n) time, where n is the length of the list.
The astype() method also takes O(n) time, since it needs to convert each element of the array to a string.
The np.char.zfill() function takes O(nk) time, where k is the length of the padded strings. Since we are padding each string to a length of K, this operation takes O(nK) time in total.
The tolist() method takes O(n) time, since it needs to convert the numpy array back to a list.
Therefore, the overall time complexity of the code is O(nK).
Auxiliary Space:
The numpy array created in step 3 requires O(nk) space, where k is the length of the padded strings. Since we are padding each string to a length of K, this array takes O(nK) space in total.
The padded list created in step 4 also requires O(nk) space, where k is the length of the padded strings. Therefore, the space complexity of the code is O(nK).
Method #5: Using a for loop and string concatenation: A simple way to pad the numbers with leading zeros is to loop over the list, convert each number to a string, and concatenate "0" characters to the left until the resulting string has the desired length.
Step-by-step approach:
The list after k size element padding: ['0003', '0054', '0004', '0100', '0010']
Time complexity: O(nk), where n is the length of the list and k is the desired length of each element.
Auxiliary Space: O(nk), where n is the length of the list and k is the desired length of each element.
Method #6: Using re module
This program takes an input list of integers and pads each integer with leading zeros to a fixed length of 4 digits. The padded integers are then stored in a new list.
Step-by-step approach:
The list after K size element padding: ['0003', '0054', '0004', '0100', '0010']
Time Complexity: O(n), The time complexity of the program is linear with respect to the number of integers in the input list, n. This is because the program performs a constant number of operations for each integer in the list.
Auxiliary Space: O(n), The space complexity of the program is also linear with respect to the number of integers in the input list, n. This is because the program creates a new list of padded integers of length n, and a string of length 4n to store the joined padded integers.
Method 7: Using the zfill() method
Step-by-step approach:
The original list is : [3, 54, 4, 100, 10] The list after K size element padding : 0003 0054 0004 0100 0010
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list (used to store the output list).