We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Python
- Basic Data Types
- Lists
- Discussions
Lists
Lists
Sort by
recency
|
2663 Discussions
|
Please Login in order to post a comment
Using eval you can streamline the coding process. This does cause an integrity issue however due to the ability of the user to submit code that will be run automatically.
if name == 'main': N=int(input()) l=[] for i in range(N): commands=[input().split()] for j in commands: v=j if v[0]=="append": l.append(int(v[1])) elif v[0]=="insert": l.insert((int(v[1])),(int(v[2]))) elif v[0]=="remove": l.remove(int(v[1])) elif v[0]=="sort": l.sort() elif v[0]=="pop": l.pop() elif v[0]=="print": print(l) else: l.reverse()
Readable, sensible
Splitting and checking for values and corresponding command using the Match-Case. The code looks way cleaner and readable compared to multiple if-elif conditions.