Python: Convert nDimensions list to string and vice versa [duplicate] - python

This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 7 years ago.
My question is probably simple but I don't be able to figure it out.
Consider this code where mylist can have any number of dimension:
mylist = [[1,2,3,4],[5,6,7]]
mylist
[[1,2,3,4],[5,6,7]]
It's easy to cconvert it to string:
myString = str(myList)
myString
>'[[1,2,3,4],[5,6,7]]'
But how to easily convert it back to the same list ?
I never get it work in any situation using .join or .split.
I want it work in any case of the list was configured.
thanks

Now mylist is a string. (from your code[enter link description here][1])
So now we pass mylist to eval.
Code Example below :
mylist=eval('[[2,3,4,5,6,2],[2,3,4,5,6,2]]')

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 can I convert List-alike stiring to an actual list? [duplicate]

This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 3 years ago.
How do I convert string that looks like this:
['Point Target', 'Channeled']
To an actual List of elements (in this example, Point Target and Channeled)?
You could use eval("['Point Target', 'Channeled']").
But be careful if you get the list from an untrusted source as eval will evaluate everything that it gets. So don't use it e.g on unfiltered input from a webinterface.

python strip function gives unexpected result [duplicate]

This question already has answers here:
How do I remove a substring from the end of a string?
(23 answers)
Closed 5 years ago.
I want to strip the substring '_pf' from a list of strings. It is working for most of them, but not where there is a p in the part of the string I want to remain. e.g.
In: x = 'tcp_pf'
In: x.strip('_pf')
Out:
'tc'
I would expect the sequence above to give an output of 'tcp'
Why doesn't it? Have i misunderstood the strip function?
you can use:
x = 'tcp_ip'
x.split('_ip')[0]
Output:
'tcp'
You can also use spilt function like below,
x.split('_pf')[0]
It will give you tcp.

Turning Input into a list [duplicate]

This question already has answers here:
Python- Turning user input into a list
(3 answers)
Create a tuple from an input in Python
(5 answers)
Closed 10 months ago.
Write a Python program which accepts a sequence of comma-separated numbers from user and generate a list and a tuple with those numbers.
values = input("Input some comma separated numbers : ")
list = values.split(",")
tuple = tuple(list)
print('List : ',list)
print('Tuple : ',tuple)
This does work but is there any other easier way?
If you're looking for a more efficient way to do this, check out this question:
Most efficient way to split strings in Python
If you're looking for a clearer or more concise way, this is actually quite simple. I would avoid using "tuple" and "list" as variable names however, it is bad practice to name variables as their type.
Well, the code that you have written is pretty concise but you could remove few more line by using the below code:
values = input("Enter some numbers:\n").split(",")
print(values) #This is the list
print(tuple(values)) #This is the tuple

Categories