Strip list of lists that have single quotes

python remove single quotes from list elements
python remove quotes from list
python remove brackets and quotes from list
remove double quotes from list python
remove single quotes from string python
remove single quotes from dictionary value python
python print list without quotes
remove single quotes around string python

I am creating a list of validation losses for a regression model and they are in this format:

mylist = ['[72.49191836014535]', '[83.83327374257702]', '[72.48327325617225]', 
 '[66.98897377186994]', '[71.13875892170039]', '[64.3820106481657]', 
 '[73.28287317220448]', '[79.7119315804787]', '[79.55777844179023]', 
 '[89.62320741326292]']

how can I put them in a simple list in order to calculate average/deviation?

Without using eval:

import numpy as np

mylist = ['[72.49191836014535]', '[83.83327374257702]', '[72.48327325617225]', 
 '[66.98897377186994]', '[71.13875892170039]', '[64.3820106481657]', 
 '[73.28287317220448]', '[79.7119315804787]', '[79.55777844179023]', 
 '[89.62320741326292]']

mylist = np.array([float(i[1:-1]) for i in mylist])

mylist.mean()

Output:

75.34939993083671

Python, Single and Double Quotes | Python � Print lists in Python (4 Different Ways) � Python | Printing String with double quotes � Python | Avoiding quotes� A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

You can use regex to remove [ and ] and then convert values to float:

import regex as re

mylist = [
    "[72.49191836014535]",
    "[83.83327374257702]",
    "[72.48327325617225]",
    "[66.98897377186994]",
    "[71.13875892170039]",
    "[64.3820106481657]",
    "[73.28287317220448]",
    "[79.7119315804787]",
    "[79.55777844179023]",
    "[89.62320741326292]",
]

data = [float(re.sub(r"[\[\]]", "", v)) for v in mylist]

Output:

[72.49191836014535,
 83.83327374257702,
 72.48327325617225,
 66.98897377186994,
 71.13875892170039,
 64.3820106481657,
 73.28287317220448,
 79.7119315804787,
 79.55777844179023,
 89.62320741326292]

How to remove single quotes from a string in Python, Kite is a free autocomplete for Python developers. Code faster with the Kite plugin for your code editor, featuring Line-of-Code Completions and cloudless� Removing multidimensional lists; Checking if string inside quotes? Quotes (') and Double-Quotes (") - Where and When to use them; newbe question about removing items from one file to another file; ascii character - removing chars from string; Need help removing list elements. List operation: Removing an item; Removing Array Elements

Use this:
import ast
[ast.literal_eval(i)[0] for i in mylist]

Full Example with numpy:

import numpy as np, ast

mylist = ['[72.49191836014535]', '[83.83327374257702]', '[72.48327325617225]', 
          '[66.98897377186994]', '[71.13875892170039]', '[64.3820106481657]', 
          '[73.28287317220448]', '[79.7119315804787]', '[79.55777844179023]', 
          '[89.62320741326292]']

clean_list = np.array([ast.literal_eval(i)[0] for i in mylist])
#array([72.49191836, 83.83327374, 72.48327326, 66.98897377, 71.13875892,
#       64.38201065, 73.28287317, 79.71193158, 79.55777844, 89.62320741])

clean_list.mean()
75.34

Full Example WITHOUT numpy:

mylist = ['[72.49191836014535]', '[83.83327374257702]', '[72.48327325617225]', 
          '[66.98897377186994]', '[71.13875892170039]', '[64.3820106481657]', 
          '[73.28287317220448]', '[79.7119315804787]', '[79.55777844179023]', 
          '[89.62320741326292]']

clean_list = [ast.literal_eval(i)[0] for i in mylist]
#[72.49191836, 83.83327374, 72.48327326, 66.98897377, 71.13875892,
#       64.38201065, 73.28287317, 79.71193158, 79.55777844, 89.62320741]

average_list = sum(clean_list) / len(clean_list)
75.34

List Manipulations, Previous Chapter: Sequential Data Types: Lists and Strings and insert objects to lists and you will also learn how to delete and remove elements by using which has at least two operations: one which can be used to put or push data on the� Mostly I make lists for projects. This can be daunting. Breaking something big into its constituent parts will help you organize your thoughts, but it can also force you to confront the depth of your ignorance and the hugeness of the task. That's OK. The project may be the lion, but the list is your whip.

Another solution, similar to above:

from statistics import mean 

mylist = ['[72.49191836014535]', '[83.83327374257702]', '[72.48327325617225]', 
          '[66.98897377186994]', '[71.13875892170039]', '[64.3820106481657]', 
          '[73.28287317220448]', '[79.7119315804787]', '[79.55777844179023]', 
          '[89.62320741326292]']

list = [ast.literal_eval(i)[0] for i in mylist]
print(mean(list))

result:

75.3493999308367

Sequence Types: Strings, Tuples and Lists, Strings can be constructed with single quotes, double quotes, or triple quotes Lists can be changed in place; Use the index to replace an element of a list with� I have one column in a list Report Name [Questions]. When I run Report [Questions] shows me:1. VSP Doctor's Name2. VSP Doctor's IDNow, I don't want this single quote (') in Questions because I need to match this column to another column. What function should I use in Cognos to Remove this (') from VSP doctor's name.Thanks

Expanding on Makis and Eric Day above:

import statistics

data = [ eval(a)[0] for a in mylist ]

# collate stats wanted for printing
stats = [sum, statistics.mean, statistics.stdev, statistics.variance]
for stat in stats:
    print(stat.___name____, stat(data))

How to convert List to String, What are Lists; What are Strings; Convert List to Strings; Convert List of integers to what if the string itself contains a single quote or double-quotes. we can use strip function to get jsy Numbers print(str(Numbers).strip('[]'))� Here are some cool being single quotes that throw light on, what being single is all about. If you’re single, you can sleep well at night; you are free to flirt. You are free to do whatever the F you want! Here are the best being single quotes, which will make you realize the benefits of living solo. 30 Being Single Quotes

Chapter 4 – Lists, Just as string values are typed with quote characters to mark where the string Just as an index can get a single value from a list, a slice can get several values from a list, in the form of a new list. Removing Values from Lists with remove(). Hi all, I am stuck with a small issue in PROC SQL. I have to fetch from a flat file & use that string in a sql query but the items in the list are strings so during the fetch i need to fetch it with quotes to use it in oracle. Any Help would be much appretiated. Thanks! CODE.txt AAA AAB AAC

Chapter 6 – Manipulating Strings, You can extract partial strings from string values, add or remove spacing, One benefit of using double quotes is that the string can have a single quote If we used the “List of Lists of Lists” example, the string stored in text would look like this:. American style is to place the comma inside the quotes. This is universally the case in publishing and accords with all style guides (Chicago, AP, NYT, etc.). The only exception is is in academic works, particularly philosophy texts, where a word is being specially defined and offset with single quotes.

3. An Informal Introduction to Python — Python 3.8.5 documentation, The string is enclosed in double quotes if the string contains a single quote and no Lists might contain items of different types, but usually the items all have the 64 >>> cubes[3] = 64 # replace the wrong value >>> cubes [1, 8, 27, 64, 125]. This is a case in which the accepted British and American English authorities appear to offer conflicting advice (but see final paragraph). In the example given in the question, the list is of a series of terms, or 'elements'.

Comments
  • Is your list really a list of strings that look like lists? Did you make this list? If so, you should really fix it at the source.
  • I agree with @MarkMeyer entirely, and I wish more people would point out that sort of thing, rather than just jumping to getting an answer of copy/paste code accepted.
  • You should use ast.literal_eval() instead of eval().
  • At least use ast.literal_eval() instead of eval().