python format list into comma separated string [duplicate] - python

This question already has answers here:
How to concatenate (join) items in a list to a single string
(11 answers)
Closed 3 years ago.
I have a simple list
li=['beststreet','borocd']
print "I like strings a lot {}".format(li)
I want to format the list into a string but I want the output to be like this:
I like strings a lot beststreet,borocd
background: this is a simple example, I am doing string manipulation in a Class to be inserted into a SQL table...

Just join the strings in the list with a comma:
li=['beststreet','borocd']
print "I like strings a lot {}".format(‘,’.join(li))

Related

remove the [''] when printing a list in python [duplicate]

This question already has answers here:
Print list without brackets in a single row
(14 answers)
Closed 5 months ago.
For some reason when I print a list like
list = []
list.append("0")
list.append("1")
print(list[0])
the output will be ["0"]
My actual code is a large block of text. Here's a link to the actual code: https://pastebin.com/Z54NfivR
Try this:
print(*list)
This essentially unpacks your list and its elements are treated as if they were separated by commas in the print function.
I used the name list because that was included in your example but it is a good practice to avoid using python commands as variable names.

Python - turn string of list of dictionaries to list of dictionaries [duplicate]

This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 8 months ago.
I have a string that looks like this:
'{"screen_name":"Brian","avatar":1},{"screen_name":"David","avatar":21},{"screen_name":"Teo","avatar":34}'
How can I make it a list of dictionaries?
I tried with json.loads, but it threw an error...
before trying json.loads, you have to make sure that is surrounded by square brackets.
You can achieve that by using format or fstrings, so you can do:
>>> import json
>>> json.loads(f"[{original}]")
[{"screen_name":"Brian","avatar":1},{"screen_name":"David","avatar":21},{"screen_name":"Teo","avatar":34}]

How to split single list element in to multiple elements based on a separator [duplicate]

This question already has answers here:
Split a string by a delimiter in python
(5 answers)
Closed 2 years ago.
I know there are a lot of similar questions but my use-case is unique. The following is my list ['apple,mango,fig']. What I want is to split the single element into multiple list elements based on ",". My final list should be like this ['apple','mango','fig']. As far as I know we can convert it into sub-list using split() method. But can I convert it into different elements of the same list? Is this possible?
If there is only one element in the list:
print(l[0].split(','))
If there are multiple elements in the list, use:
print([x for i in l for x in i.split(',')])

Removing single quotes from a list of string [duplicate]

This question already has answers here:
I want to replace single quotes with double quotes in a list
(3 answers)
Closed 3 years ago.
I have a list of products which are strings & want to remove single quotes from it
The list of products retrieved from a sql query in python environment
['000003286694', '000003286704', '000003420412']
I have to pass this list as a payload to json object. So it needs to be preceded by double quotes.
I have used following method
d=[('"'+i+'"') for i in x]
but I get the output as
['"000003286694"', '"000003286704"', '"000003420412"']
I tried using regex, replace method to get away with single quotes but no success.
Can anyone please share workaround for this
I want result list should be
["000003286694", "000003286704", "000003420412"]
data =['000003286694', '000003286704', '000003420412']
import json
result =json.dumps(data)
print(result) #["000003286694", "000003286704", "000003420412"]

Filter list and print whole name containing string python [duplicate]

This question already has answers here:
Filtering a list of strings based on contents
(5 answers)
Closed 3 years ago.
I am trying to filter a list in python.
What i need is a way to limit what is beeing printed out from the list
Example:
I want to find all subjects containing "ECON"
from this list:
List = ["INFO100","INFO104","INFO110","INFO150","INFO125","ECON100", "ECON102"]
And i want to be able to print out the full name of the objects containing "ECON" (that means i want it to return "ECON100", "ECON102")
is there an easy way to do this?
for sub_string in List:
if "ECON" in sub_string:
print(sub_string)

Categories