Python – Convert Alternate String Character to Integer
Interconversion between data types is facilitated by python libraries quite easily. But the problem of converting the alternate list of string to integers is quite common in development domain. Let’s discuss few ways to solve this particular problem.
Method #1 : Naive Method This is most generic method that strikes any programmer while performing this kind of operation. Just looping over whole list and convert alternate of the string of list to int by type casting.
Python3
test_list = [ '1' , '4' , '3' , '6' , '7' ]
print ( "Original list is : " + str (test_list))
for i in range ( 0 , len (test_list)):
if i % 2 :
test_list[i] = int (test_list[i])
print ( "Modified list is : " + str (test_list))
|
Output
Original list is : ['1', '4', '3', '6', '7']
Modified list is : ['1', 4, '3', 6, '7']
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 list
Method #2 : Using list comprehension This is just a kind of replica of the above method, just implemented using list comprehension, kind of shorthand that a developer looks for always. It saves time and complexity of coding a solution.
Python3
test_list = [ '1' , '4' , '3' , '6' , '7' ]
print ( "Original list is : " + str (test_list))
test_list = [ int (ele) if idx % 2 else ele for idx, ele in enumerate (test_list)]
print ( "Modified list is : " + str (test_list))
|
Output
Original list is : ['1', '4', '3', '6', '7']
Modified list is : ['1', 4, '3', 6, '7']
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 map() function:
The map() function to apply the int() function to every other element of the list using slicing, effectively converting every other element to an integer.
Python3
test_list = [ '1' , '4' , '3' , '6' , '7' ]
print ( 'Original list is :' + str (test_list))
test_list[ 1 :: 2 ] = list ( map ( int ,test_list[ 1 :: 2 ]))
print ( 'Modified list is : ' + str (test_list))
|
Output
Original list is :['1', '4', '3', '6', '7']
Modified list is : ['1', 4, '3', 6, '7']
Time Complexity: O(n) where n is the number of elements in the string list. The map() 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 test list.
Method#4: Using the zip() function to alternate the elements in the list
Python3
test_list = [ '1' , '4' , '3' , '6' , '7' ]
print ( 'Original list is :' + str (test_list))
test_list = [x if i % 2 = = 0 else int (x) for i,x in zip ( range ( len (test_list)),test_list)]
print ( 'Modified list is : ' + str (test_list))
|
Output
Original list is :['1', '4', '3', '6', '7']
Modified list is : ['1', 4, '3', 6, '7']
Time complexity: O(n) where n is the length of the input list
Auxiliary Space: O(n) as a new list of the same length is created during the conversion.
Method #5: Using enumeration
Algorithm:
- Initialize the input list test_list.
- Print the original list.
- Use enumerate() to iterate over each element of test_list with its index.
- For each element, check whether the index is even or odd.
- If the index is even, skip the element using the continue statement.
- If the index is odd, convert the element to an integer using int() and replace the original element in test_list with the new integer value.
- Print the modified list.
Python3
test_list = [ '1' , '4' , '3' , '6' , '7' ]
print ( "Original list is : " + str (test_list))
for i, ele in enumerate (test_list):
if i % 2 = = 0 :
continue
test_list[i] = int (ele)
print ( "Modified list is : " + str (test_list))
|
Output
Original list is : ['1', '4', '3', '6', '7']
Modified list is : ['1', 4, '3', 6, '7']
Time complexity: O(n), where n is the length of the input list test_list. This is because the for loop and if statement iterate over each element of the list exactly once. The time complexity is proportional to the length of the input list.
Auxiliary Space: O(1), because the code modifies the input list in place without creating any additional data structures. Therefore, the space used by the algorithm is constant and independent of the length of the input list.
Method #6: Using a for loop with a range
This approach uses a for loop with a range to iterate over every other index in the list and convert the corresponding element to an integer.
Python3
test_list = [ '1' , '4' , '3' , '6' , '7' ]
print ( "Original list is : " + str (test_list))
for i in range ( 1 , len (test_list), 2 ):
test_list[i] = int (test_list[i])
print ( "Modified list is : " + str (test_list))
|
Output
Original list is : ['1', '4', '3', '6', '7']
Modified list is : ['1', 4, '3', 6, '7']
Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(1), since we are modifying the original list in-place.
Last Updated :
13 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...