Open In App

Python – Convert Alternate String Character to Integer

Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python 3 code to demonstrate
# Alternate String to Integer Conversion
# using naive method
 
# initializing list
test_list = ['1', '4', '3', '6', '7']
 
# Printing original list
print ("Original list is : " + str(test_list))
 
# using naive method to
# perform conversion
for i in range(0, len(test_list)):
    if i % 2:
        test_list[i] = int(test_list[i])
     
# Printing modified 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 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




# Python 3 code to demonstrate
# Alternate String to Integer Conversion
# using list comprehension
 
# initializing list
test_list = ['1', '4', '3', '6', '7']
 
# Printing original list
print ("Original list is : " + str(test_list))
 
# using list comprehension to
# perform conversion
test_list = [int(ele) if idx % 2 else ele for idx, ele in enumerate(test_list)]
     
# Printing modified 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




# Python 3 code to demonstrate
# Alternate String to Integer Conversion
# using map() function
 
# initializing list
test_list = ['1', '4', '3', '6', '7']
 
# Printing original list
print ('Original list is :' + str(test_list))
 
# using map() function to
# perform conversion
test_list[1::2] = list(map(int,test_list[1::2]))
 
 
# Printing modified 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 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']
 
#Printing original list
print ('Original list is :' + str(test_list))
 
#using zip() function to alternate the elements
test_list = [x if i%2==0 else int(x) for i,x in zip(range(len(test_list)),test_list)]
 
#Printing modified list
print ('Modified list is : ' + str(test_list))
#This code is contributed by Edula Vinay Kumar Reddy


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:

  1. Initialize the input list test_list.
  2. Print the original list.
  3. Use enumerate() to iterate over each element of test_list with its index.
  4. For each element, check whether the index is even or odd.
  5. If the index is even, skip the element using the continue statement.
  6. 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.
  7. Print the modified list.

Python3




# initializing list
test_list = ['1', '4', '3', '6', '7']
 
# Printing original list
print("Original list is : " + str(test_list))
 
# using enumerate()
# perform conversion
for i, ele in enumerate(test_list):
    if i % 2 == 0:
        continue
    test_list[i] = int(ele)
     
# Printing modified list
print("Modified list is : " + str(test_list))
#This code is contributed by Vinay Pinjala.


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




# initializing list
test_list = ['1', '4', '3', '6', '7']
 
# Printing original list
print("Original list is : " + str(test_list))
 
# using for loop with range()
# perform conversion
for i in range(1, len(test_list), 2):
    test_list[i] = int(test_list[i])
     
# Printing modified 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 list.
Auxiliary space: O(1), since we are modifying the original list in-place.



Last Updated : 13 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads