+ 4
How can i create a LIST, DICTIONARY and TUPLE with user input?
6 Answers
+ 6
Lists and tuples can be easily generated from strings depending on how you wish to split them. A primitive example:
usr_inp = input()
print(list(usr_inp))
print(tuple(usr_inp))
A dictionary is tricker. You can rely on literal_eval
https://docs.python.org/3/library/ast.html#ast.literal_eval
or just parse it yourself if you can be sure that the input string will always conform to how a dict is represented in Python.
https://stackoverflow.com/questions/988228/convert-a-string-representation-of-a-dictionary-to-a-dictionary
+ 5
On SL you can try the following way of getting the user to enter a dictionary in python.
out_dct={i.strip():j.strip() for i,j in [tuple(k.split(':')) for k in input().split(',') ]}
print(out_dct)
Enter text on one line as:
a : apple juice, b : banana juice , c : carrot juice
https://code.sololearn.com/c6YFNWDMNm06/?ref=app
+ 2
One element at a time đ€
+ 1
To show in SL how your code works you can use samples or random data.
0
mm not entirely sure what you mean, but you can use the exec function to execute python code based on input and string value (in this example I entered the value of âtestâ in input to create a list)
createlist = input() + " = []"
exec(createlist)
#I entered âtestâ as input which created a list named test
test.append("hi")
print(test)
however, I would assume this method should be avoided if at all possible...
- 1
Please someone help me. I am a beginner who wants to become a professional web developer and a design as well. What skill do i need and where can i learn it better?