It is already a list:
>>> type(my_set)
<class 'list'>
Do you want something like:
>>> my_set = set([1, 2, 3, 4])
>>> my_list = list(my_set)
>>> print(my_list)
[1, 2, 3, 4]
EDIT: Output of your last comment:
>>> my_list = [1,2,3,4]
>>> my_set = set(my_list)
>>> my_new_list = list(my_set)
>>> print(my_new_list)
[1, 2, 3, 4]
I'm wondering if you did something like this:
>>> set = set()
>>> set([1, 2])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'set' object is not callable
Answer from user on Stack OverflowHow to convert a set to a list in python? - Stack Overflow
java - How to Convert List<String> to List<Object> - Stack Overflow
converting a string that is list-like into a list
python - How to convert string representation of list to a list - Stack Overflow
It is already a list:
>>> type(my_set)
<class 'list'>
Do you want something like:
>>> my_set = set([1, 2, 3, 4])
>>> my_list = list(my_set)
>>> print(my_list)
[1, 2, 3, 4]
EDIT: Output of your last comment:
>>> my_list = [1,2,3,4]
>>> my_set = set(my_list)
>>> my_new_list = list(my_set)
>>> print(my_new_list)
[1, 2, 3, 4]
I'm wondering if you did something like this:
>>> set = set()
>>> set([1, 2])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'set' object is not callable
Instead of:
first_list = [1,2,3,4]
my_set=set(first_list)
my_list = list(my_set)
Why not shortcut the process:
my_list = list(set([1,2,3,4])
This will remove the dupes from you list and return a list back to you.
Pass the List<String> as a parameter to the constructor of a new ArrayList<Object>.
List<Object> objectList = new ArrayList<Object>(stringList);
Any Collection can be passed as an argument to the constructor as long as its type extends the type of the ArrayList, as String extends Object. The constructor takes a Collection, but List is a subinterface of Collection, so you can just use the List<String>.
Any java collection is just a collection of objects be it string or other. The type argument is just sugar. Depending on situation, such as dealing with very large lists, you may just want to convert it - obviously risking mixing two different types of objects in the same list.
List<Object> objectList = (List)stringList;
And put a @SuppressWarning to get rid of nasties...
Hopefully I can explain this properly. I have no control over how the input string which is kind of formatted like a list of lists but without quotes (so I can't use json.loads)
Example input (I'm reading it in using sys.argv):
"[[a,1],[b,2]]"
I'd like to convert this to be a real list of lists so I can iterate through them in my script.
I can get what I need using split but I was wondering if there was a better way to do it. Either a better way to divide them or a way to add quotes - whatever works.
import sys
try1 = sys.argv[1]
lst1 = try1.split("],")
print("try #1 (works but could be better?)")
print(len(lst1))
for item in lst1:
print(item.lstrip("[[").rstrip("]]"))
# doesn't work - 1 item in the list
print("\n\nTry #2")
try2 = sys.argv[1:]
print(len(try2))
# doesn't work - length of 13
print("\n\nTry #3")
print(len(list(sys.argv[1])))Output
$ python3 t.py "[[a,1],[b,2]]" try #1 (works but could be better?) 2 a,1 b,2 Try #2 1 Try #3 13
Thanks for reading
>>> import ast
>>> x = '[ "A","B","C" , " D"]'
>>> x = ast.literal_eval(x)
>>> x
['A', 'B', 'C', ' D']
>>> x = [n.strip() for n in x]
>>> x
['A', 'B', 'C', 'D']
ast.literal_eval:
Evaluate an expression node or a string containing only a Python literal or container display. The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans,
NoneandEllipsis.This can be used for evaluating strings containing Python values without the need to parse the values oneself. It is not capable of evaluating arbitrarily complex expressions, for example involving operators or indexing.
The json module is a better solution whenever there is a stringified list of dictionaries. The json.loads(your_data) function can be used to convert it to a list.
>>> import json
>>> x = '[ "A","B","C" , " D"]'
>>> json.loads(x)
['A', 'B', 'C', ' D']
Similarly
>>> x = '[ "A","B","C" , {"D":"E"}]'
>>> json.loads(x)
['A', 'B', 'C', {'D': 'E'}]
This is a weird one but I have an output from a csv that is a list but one reading it from the list it becomes a string and I want to to convert that back into a list, any ideas?
"ingredients": "['asparagus', 'olive oil', 'salt', 'unsalted butter', 'sage leaves', 'fresh lemon juice', 'lemon, zest of', 'parmigiano-reggiano cheese']",