How to append a list withoud adding the quote
Well, I know two possible ways, but the first one is faster:
1:
def add_peer_function():
all_devices=[item for item in "cisco,linux".split(',')] # or `all_devices = ["cisco", "linux"]`
print(', '.join(all_devices)) # A prettier way to print list Thanks to Philo
add_peer_function()
2:
def add_peer_function():
all_devices=[]
for item in "cisco,linux".split(','): # or `all_devices = ["cisco", "linux"]`
all_devices.append(item)
print(', '.join(all_devices)) # A prettier way to print list Thanks to Philo
add_peer_function()
Python str.split documentation.
Python str.join documentation.
Python list comprehension documentation.
Python Append String inside list just as an element without double quotes
I believe you are confusing putting a list inside a list and concatenating two lists:
[1, 2, paramlist, 5] # [1, 2, [3, 4], 5]
is not the same as
[1, 2] + paramlist + [5] # [1, 2, 3, 4, 5]
Instead of using the correct operation you are trying to cast the list to a string to manually remove the square brackets. Needless to say this is the wrong way of doing it.
So i think you are looking for
value4 = [{'PARAMS': ['ProcessingDate=2016-08-29', 'ReRun=Y']}]
paramlist = value4[0]['PARAMS']
paramlist = [elem for x in paramlist for elem in ('-param', x)]
myNewlist = ['Value1', 'value2', 'value3'] + paramlist + ['value5']
print (myNewlist)
which yields
['Value1', 'value2', 'value3', '-param', 'ProcessingDate=2016-08-29', '-param', 'ReRun=Y', 'value5']
Add to python list without quotations
Use ast.literal_eval to parse the string "{'key1': 1, 'key2': 2, 'key3': 3}"
to a dictionary.
In [16]: import ast
In [17]: classes = {}
In [18]: classes["class1"] = ast.literal_eval("{'key1': 1, 'key2': 2, 'key3': 3}")
In [19]: classes
Out[19]: {'class1': {'key1': 1, 'key2': 2, 'key3': 3}}
Note that you cannot use json.loads here due to the single quotes in your string
In [20]: import json
In [21]: classes = {}
In [22]: classes["class1"] = json.loads("{'key1': 1, 'key2': 2, 'key3': 3}")
---------------------------------------------------------------------------
JSONDecodeError Traceback (most recent call last)
<ipython-input-22-592615e01642> in <module>
----> 1 classes["class1"] = json.loads("{'key1': 1, 'key2': 2, 'key3': 3}")
JSONDecodeError: Expecting property name enclosed in double quotes:
line 1 column 2 (char 1)
Prevent outer double quotes in my list
Use ast.literal_eval to convert a string representation of Python data to the actual data structure:
>>> from ast import literal_eval
>>> s = "('x',),('y',)"
>>> literal_eval(s)
(('x',), ('y',))
literal_eval works similarly to eval
but can only produce literal Python data from strings: strings, numbers, tuples, lists, dicts, booleans, and None. Quite a bit safer than eval
alone.
If you actually want a list vs a tuple from your string:
>>> list(literal_eval(s))
[('x',), ('y',)]
Is it possible to remove all single quotes from a list of strings in python?
If you are looking for datetime
, You don't want to use string, Try something like this,
In [12]: import datetime
In [13]: datelist = [datetime.datetime(yearlist[i],monthlist[i],daylist[i]) for i in range(0,j)]
Result
[datetime.datetime(2009, 1, 14, 0, 0), datetime.datetime(2009, 2, 15, 0, 0)]
Removing quotation marks from list items
Try a list comprehension which allows you to effectively and efficiently reassign the list to an equivalent list with quotes removed.
g1 = [i.replace('"', '') for i in g1] # remove quote from each element
How to remove double quotes from list of strings?
Try this:
VERSION = ["'pilot-2'", "'pilot-1'"]
VERSIONS_F = []
for item in VERSION:
temp = item.replace("'",'')
VERSIONS_F.append(temp)
print (VERSIONS_F)
it will print ['pilot-2','pilot-1']
Related Topics
Python Creating Dictionary from Excel Data
Invalidargumenterror: Logits and Labels Must Have the Same First Dimension Seq2Seq Tensorflow
Join Two Dataframes from a Conditional Row
How to Sum Multiple Columns in a Spark Dataframe in Pyspark
Skip First Couple of Lines While Reading Lines in Python File
How to Completely Remove Python from a Windows Machine
How to Plot Date and Time in X Axis Against Y Value (Python)
How to Fill in Arbitrary Missing Dates in Pandas Dataframe
Convert CSV to Parquet File Using Python
Get List of Files in a Sharepoint Directory Using Python
How to Iterate Through a List of Dictionaries in Jinja Template
How to Split But Ignore Separators in Quoted Strings, in Python
How to Get the Sum of a List of Numbers With Recursion
How to Convert a Django Queryset into List of Dicts
Pandas Get the Most Frequent Values of a Column