Python

Python Convert List of Strings to Ints

You must have done some type-casting in different programming languages, such as the conversion of one data type to another. Typecasting has also been supported by the Python language. However, the conversion of the whole list or dictionary to another data type is quite difficult for beginners. Therefore, this guide has been specially implemented to convert a string-type list to an integer-type list in Spyder 3.

Example 01: Using Naïve Method

Our first example would be implementing the naïve method to convert a list of strings to an integer. It is identified as the most generic method to convert a string-type list to an integer-type list. It has been achieved by applying a simple loop using type casting on the list for conversion. So, we have created a new Python project and initialized a list called “List” of five string elements. This list has been displayed using the print function. After that, we have set up a “for” loop that runs from 0 to the list’s length. On every iteration, the element of the list “List” has been converted into the integer type using type casting and saved to the same index of list “List”. An updated type cast new list has been printed out, as shown:

  • List = [‘1’, ‘3’, ‘5’, ‘7’, ‘9’]
  • print (“String list: ” + str(List))
  • for i in range(0, len(List)): # naïve method
  • List[i] = int(List[i])
  • print (“Integer list: ” + str(List))

Run the previous code by utilizing the “Run” button of the Spyder 3 held at the menu bar. After running this file, we have the following result. The string type old list has been displayed first, and after that, the new and type casted list has been displayed on the console, such as string type and list type:

Example 02: Using Map Function

Our second and most efficient method to convert a string-type list to an integer-type list is using the map() function in our code. The map() method has been used specifically for conversion. So, within the Python code, we have defined an integer type list. The print function has been printing the original string-type list, such as List. The map function has been used to convert the index to an integer by passing it as an argument on the following line. This method takes every single element of a string separately to convert. After conversion, the data would be converted into a list once again. The resultant list would be saved into the variable “result”. At the last line, the updated integer-type list would be displayed using the print clause:

  • List = [’45’, ‘9’, ‘-63′, ’37’, ‘-88’]
  • print (“String type list: ” + str(List))
  • result = list(map(int, List))
  • print (“Integer type list: ” , result)

After running the code, we have the string-type list and integer-type list on the output console, as shown in the image.

Example 03: List Comprehension Method

This method is quite similar to the first method but somewhat direct to convert a string-type list to an integer-type list. We have started this example by initializing a string-type list having different values in it. Firstly, the string list would be showed on the console utilizing the print statement. Then, we have applied the list comprehension method on the list to get it converted into an integer-type list. Each value at index “I” of a list would be converted into an integer using the “int(i)” clause of for loop. A new list “List” would be inserted with the updated values. The integer-type list is then printed out in the console of Spyder 3 using the “List” in the parameter of a print() function:

  • List = [’45’, ‘9’, ‘-63′, ’37’, ‘-88’]
  • print (“String type list: ” + str(List))
  • List = [int(i) for I in List]
  • print (“Integer type list: ” , List)

After running the code, we have the string-type list first, and after that, we have got the integer-type list:

Conclusion:

Finally, we have covered the topic of converting a string-type list to an integer-type list. We have demonstrated the three different methods to illustrate the examples of conversion. Against every method, we have provided illustrations to define differences among them. We believe this tutorial will be helpful for all Python users, i.e., basic and advanced. We hope you found this article helpful.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content