• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
[Tut] How to Convert a Float List to a String List in Python

How to Convert a Float List to a String List in Python

The most Pythonic way to convert a list of floats `fs` to a list of strings is to use the one-liner `fs = [str(x) for x in fs]`. It iterates over all elements in the list `fs` using list comprehension and converts each list element `x` to a string value using the `str(x)` constructor.

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

Problem: Given a list of floats `[1.0, 2.0, 3.0]`. How to convert it to a list of strings `["1.0", "2.0", "3.0"]`?

## Method 1: List Comprehension

Suppose we have a list:

`a = [1.1, 1.2, 1.8, 0.5, 5.9, -2.3]`

Now, check the type of the list numbers:

```print(type(a[0]))
# <class 'float'>
```

Let’s apply the built-in function `str()`, and get a list of strings using list comprehension:

```print([str(a) for a in a])
# ['1.1', '1.2', '1.8', '0.5', '5.9', '-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 = [str(a) for a in a]
print(type(A[0]))
# <class 'str'>```

The built-in function `str` converts a float to a string representation of that float. Thus, it helps us create a new list of strings from the list of floats 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.1, 1.2, 1.8, 0.5, 5.9, -2.3]
print(list(map(str, a)))
# ['1.1', '1.2', '1.8', '0.5', '5.9', '-2.3']```

The `map()` function returns an iterable `map` object that we need to convert to a list using the built-in `list()` constructor.

## Method 3: For Loop

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

```a = [1.1, 1.2, 1.8, 0.5, 5.9, -2.3]
strings = [] for element in a: strings.append(str(element)) print(strings)
# ['1.1', '1.2', '1.8', '0.5', '5.9', '-2.3']```

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

• Create an empty list with `strings = []`.
• Iterate over each float element using a `for` loop such as `for element in list`.
• Convert the float to a string using `str(element)` and append it to the new string list using the `list.append()` method.

## Method 4: String Formatting for Custom String Conversions

If this is not enough for you, for instance, you need a specific format of the converted strings such as only two digits after the decimal point, you should have a look at Python’s powerful string formatting capabilities.

For example, to convert a list of floats with many digits to a list of strings with only two digits, use the `string.format()` method:

```a = [1.11111, 1.200023402, 1.844333, -0.5000001]
strings = ['{:.2f}'.format(x) for x in a]
print(strings)
# ['1.11', '1.20', '1.84', '-0.50']```

Python’s built-in `format(value, spec)` function transforms input of one format into output of another format defined by you. Specifically, it applies the format specifier `spec` to the argument `value` and returns a formatted representation of `value`. For example, `format(42, 'f')` returns the string representation `'42.000000'`.

You can watch me introducing the formatting capabilities in this short guide:

To boost your Python skills the easy way, feel free to join my free email academy with lots of free content and cheat sheets—if you haven’t already!

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

The post How to Convert a Float List to a String List in Python first appeared on Finxter.

https://www.sickgaming.net/blog/2021/04/...in-python/

 Possibly Related Threads… Thread Author Replies Views Last Post [Tut] How to Check if a Python String Contains Only Digits? xSicKxBot 0 8 04-12-2021, 12:23 PM Last Post: xSicKxBot [Tut] How to Convert a String List to an Integer List in Python xSicKxBot 0 12 04-09-2021, 01:50 PM Last Post: xSicKxBot [Tut] Python String to Float Conversion xSicKxBot 0 20 04-01-2021, 11:07 PM Last Post: xSicKxBot [Tut] How to Convert Hex String to Integer in Python xSicKxBot 0 21 03-30-2021, 01:04 PM Last Post: xSicKxBot [Tut] Python OpenCV Image Processing – Resize, Blend, Blur, Threshold, Convert xSicKxBot 0 22 03-23-2021, 12:13 PM Last Post: xSicKxBot [Tut] Python String zfill() xSicKxBot 0 19 03-22-2021, 06:30 PM Last Post: xSicKxBot [Tut] Check If All Elements In A List Are Identical xSicKxBot 0 22 03-20-2021, 10:12 AM Last Post: xSicKxBot [Tut] How to Loop Through a Python List in Pairs, Sliding Windows, and Batches? xSicKxBot 0 29 03-17-2021, 03:08 PM Last Post: xSicKxBot [Tut] How to Create a Tuple from a String and a List of Strings xSicKxBot 0 30 03-13-2021, 09:55 AM Last Post: xSicKxBot [Tut] How to Replace One or More List Elements at Specific Indices in Python? xSicKxBot 0 29 03-09-2021, 05:03 PM Last Post: xSicKxBot

Forum Jump:

 Active Threads Unigine 2.14 Released Last Post: xSicKxBot Today 03:06 PM » Replies: 0 » Views: 6 News - Lawson Will Offer New Pokémon Sna... Last Post: xSicKxBot Today 03:06 PM » Replies: 0 » Views: 4 News - Nacon Announces WRC 10, Speeds On... Last Post: xSicKxBot Today 03:06 PM » Replies: 0 » Views: 3 News - Blizzard Arcade Collection Gets T... Last Post: xSicKxBot Today 03:06 PM » Replies: 0 » Views: 7 (Indie Deal) FREE Stranded In Time | XCO... Last Post: xSicKxBot Today 07:54 AM » Replies: 0 » Views: 9 Microsoft - 5 reasons to attend Azure St... Last Post: xSicKxBot Today 07:54 AM » Replies: 0 » Views: 8 News - Deals: Ring Fit Adventure Is Down... Last Post: xSicKxBot Today 07:54 AM » Replies: 0 » Views: 9 News - Cuphead Will Have An Awesome Deca... Last Post: xSicKxBot Today 07:54 AM » Replies: 0 » Views: 9 News - Nintendo Actually Responded To A ... Last Post: xSicKxBot Today 01:50 AM » Replies: 0 » Views: 9 News - Rumour: Dragon Ball Z: Kakarot Co... Last Post: xSicKxBot Today 01:50 AM » Replies: 0 » Views: 9