Convert List int to int c#

The most Pythonic way to convert a list of strings to a list of ints is to use the list comprehension [int[x] for x in strings]. It iterates over all elements in the list and converts each list element x to an integer value using the int[x] built-in function.

This article shows you the simplest ways to convert a one-dimensional list consisting only of strings to a list of ints.

Problem: Given a list of strings ["1", "2", "-3"]. How to convert it to a list of ints [1, 2, -3]?

  • Problem Variant: Given a list of strings with mixed representations ["1", "2.0", "-3.4"]. How to convert it to a list of ints [1, 2, -3]?

Well dive into the easier base problem first and examine the problem variant in Method 5.

Table of Contents

  • Method 1: List Comprehension
  • Method 2: Map Function
  • Method 3: For Loop
  • Method 4: List Comprehension + eval[]
  • Method 5: Mixed String Representation with Rounding

Method 1: List Comprehension

Suppose we have a list:

a = ["1", "2", "-3"]

Now, check the type of the first list element:

print[type[a[0]]] #

Lets apply the built-in function int[], and get a list of integers using list comprehension:

a = ["1", "2", "-3"] print[[int[x] for x in a]] # [1, 2, -3]

?List comprehension is a compact way of creating lists. The simple formula is [expression + context]. Expression: What to do with each list element? Context: What elements to select? The context consists of an arbitrary number of for and if statements.

You can watch me explain list comprehensions in this video:

Check the type of numbers in the new list:

A = [int[x] for x in a] print[type[A[0]]] #

The built-in function int[] converts a string to an integer. Thus, it helps us create a new list of ints from the list of strings in a single line of code.

Method 2: Map Function

The built-in function map is well optimized and efficient, when it is called, the elements of the list are retrieved upon access. Therefore, one element is stored and processed in memory, which allows the program not to store the entire list of elements in the system memory.

Apply to the same list a the following code:

a = ["1", "2", "-3"] print[list[map[int, a]]] # [1, 2, -3]

?The map[] function applies the first argument, a function, to each element in an iterable. It transforms each element in the original iterable to a new element and returns a new iterable map object of transformed values. To obtain a list, you need to convert it using the built-in list[] constructor.

You can watch my explainer video of the map function here:

Method 3: For Loop

Of course, you can also convert a list of strings to a list of ints using a simple for loop. This is what most people coming from a programming language such as Java and C++ would do as they dont know the most Pythonic way of using list comprehension, yet [see Method 1].

a = ["1", "2", "-3"] ints = [] for element in a: ints.append[int[element]] print[ints] # [1, 2, -3]

This basic method to convert a list of strings to a list of integers uses three steps:

  • Create an empty list with ints = [].
  • Iterate over each string element using a for loop such as for element in list.
  • Convert the string to an integer using int[element] and append it to the new integer list using the list.append[] method.

Method 4: List Comprehension + eval[]

You can also use the eval[] function in a list comprehension to convert a list of strings to a list of ints:

a = ["1", "2", "-3"] ints = [eval[x] for x in a] print[ints] # [1, 2, -3]

?Pythons built-in eval[s] function parses the string argument s into a Python expression, runs it, and returns the result of the expression. If the expression is a simple integer representation, Python converts the argument s to an integer.

You can watch me introducing the ins and outs of the eval[] function in this short guide:

Method 5: Mixed String Representation with Rounding

Problem Variant: Given a list of strings with mixed representations ["1", "2.0", "-3.4", "3.6"]. How to convert it to a list of ints [1, 2, -3, 4]?

The challenge is to convert each string to a float first and only then convert it to an integer. These two steps are integral and none can be skipped because you need the float to be capable of representing any number. But you also need the integer as this is the goal you set out to do: converting a list of strings to a list of ints.

Convert a list of mixed string representations to a list of rounded integers this by chaining the built-in functions round[] and float[] in a list comprehension expression [round[float[s]] for s in a], assuming that the list of mixed string representation is stored in the variable a.

a = ["1", "2.0", "-3.4", "3.6"] ints = [round[float[s]] for s in a] print[ints] # [1, 2, -3, 4]

You can learn everything about the round[] function in the following video:

To boost your Python skills the easy way, feel free to join my free email academy with lots of free content and cheat sheetsif you havent already!

If you want to go all-in and learn Python while getting paid in the process, check out my Python freelancer coursethe number one freelance developer education in the world!

Video liên quan

Chủ Đề