Open In App

Python | Construct N Range Equilength String list

Last Updated : 17 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python, we can have a problem in which we need to construct a string list which contains N range numbers. But we have requirement in which we need to keep each element of equal length. Let’s discuss certain ways in which this task can be performed. 

Method #1: Using list comprehension + zfill() The combination of above functionalities can be used to perform this task. In this, we perform the task of adding numbers using list comprehension and zfill() takes care of length required for each string. 

Python3




# Python3 code to demonstrate working of
# Construct N Range Equilength String list
# using list comprehension + zfill()
 
# initialize N
N = 6
 
# printing N
print("Number of elements required : " + str(N))
 
# initialize K
K = 3
 
# Construct N Range Equilength String list
# using list comprehension + zfill()
res = [str(ele).zfill(K) for ele in range(N)]
     
# printing result
print("K Length range strings list : " + str(res))


Output : 

Number of elements required : 6
K Length range strings list : ['000', '001', '002', '003', '004', '005']

Time Complexity: O(n), where n is the length of the input list. This is because we’re using list comprehension + zfill() which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.

  Method #2 : Using map() + string formatting This task can also be performed using above functions. In this, we extend the logic of length using string formatting. And is used for constructing the N range. 

Python3




# Python3 code to demonstrate working of
# Construct N Range Equilength String list
# using  map() + string formatting
 
# initialize N
N = 6
 
# printing N
print("Number of elements required : " + str(N))
 
# initialize K
K = 3
 
# Construct N Range Equilength String list
# using  map() + string formatting
temp = '{:0' + str(K) + '}'
res = list(map(temp.format, range(N)))
     
# printing result
print("K Length range strings list : " + str(res))


Output : 

Number of elements required : 6
K Length range strings list : ['000', '001', '002', '003', '004', '005']

Time Complexity: O(n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list 

Method #3: Using List Comprehension + format method

Python3




# Python3 code to demonstrate working of
# Construct N Range Equilength String list
# using format method
   
# initialize N
N = 6
   
# printing N
print("Number of elements required : " + str(N))
   
# initialize K
K = 3
   
res = ['{:0{}}'.format(i, K) for i in range(N)]
# printing result
print("K Length range strings list : " + str(res))
#this code is contributed by edula vinay kumar reddy


Output

Number of elements required : 6
K Length range strings list : ['000', '001', '002', '003', '004', '005']

Time complexity: O(n)

Auxiliary Space: O(n)

Method #4: Using numpy:

Algorithm:

  1. Initialize N and K with integer values.
  2. Create a numpy array by using np.arange(N) which generates the range of numbers from 0 to N-1.
  3. Convert the numpy array elements to strings by using astype(str).
  4. Pad the left side of each string element with zeroes until the length of the string becomes K by using np.char.zfill() function.
  5. Convert the resulting numpy array back to a list by using tolist() method.
  6. Print the final list.

Python3




import numpy as np
 
# initialize N
N = 6
# printing N
print("Number of elements required : " + str(N))
# initialize K
K = 3
 
# create a numpy array of integers from 0 to N-1
arr = np.arange(N)
 
# convert the array of integers to an array of strings
# using astype() function of numpy
arr_str = arr.astype(str)
 
# use np.char.zfill() function to add leading zeros
# to each element of the array to make them K-length
res = np.char.zfill(arr_str, K)
 
# convert the numpy array back to a regular Python list
res_list = res.tolist()
 
# print the final list
print("K Length range strings list : " + str(res_list))
#This code is contributed by Jyothi pinjala


Output:
Number of elements required : 6
K Length range strings list : ['000', '001', '002', '003', '004', '005']

Time Complexity:

The time complexity of the algorithm is O(N) because we need to perform the following operations:

np.arange(N) – generates the range of numbers from 0 to N-1 which takes O(N) time.
astype(str) – converts the numpy array elements to strings which takes O(N) time.
np.char.zfill() – pads the left side of each string element with zeroes until the length of the string becomes K which takes O(N) time.
tolist() – converts the resulting numpy array back to a list which takes O(N) time.
Therefore, the overall time complexity of the algorithm is O(N).
Auxiliary Space:

The space complexity of the algorithm is also O(N) because we are creating a numpy array to hold the range of numbers from 0 to N-1, and then converting it to a list after padding each element with zeroes. Therefore, the overall space complexity of the algorithm is O(N).



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads