If you do not like to write manually lists with items into them with python or you want to copy and paste lists in python withou having to put the commas and the apostrophes around each item, you can find different shortcuts. If you have an IDE you can easily find some shortcut to do it, but it will alway get some time to do it. As an alternative you can use this script that creates a GUI with tkinter for python of course.
import tkinter as tk def create_list(inpw, outw): content = inpw.get("0.0", tk.END) content = content.splitlines() for i in content: if i == "": index = content.index(i) content.pop(i) print(content) content = "mylist = [" + ",".join(content) + "]" print(content) outw.insert("0.0", content) # Widgets class Start: global text, text2 root = tk.Tk() text = tk.Text(root) text.pack(side="left") text2 = tk.Text(root) text2.pack(side="left") root.bind("<Control-b>", lambda event: create_list(text, text2)) root.mainloop()
This is what you get
It is useful when lists are long and you get them from a copy and paste from somewhere like this example here:
Say we want to make this list of built-in functions in python we get from the page of the documentation
mylist = [abs(),,delattr(),,hash(),,memoryview(),,set(),,all(),,dict(),,help(),,min(),,setattr(),,any(),,dir(),,hex(),,next(),,slice(),,ascii(),,divmod(),,id(),,object(),,sorted(),,bin(),,enumerate(),,input(),,oct(),,staticmethod(),,bool(),,eval(),,int(),,open(),,str(),,breakpoint(),,exec(),,isinstance(),,ord(),,sum(),,bytearray(),,filter(),,issubclass(),,pow(),,super(),,bytes(),,float(),,iter(),,print(),,tuple(),,callable(),,format(),,len(),,property(),,type(),,chr(),,frozenset(),,list(),,range(),,vars(),,classmethod(),,getattr(),,locals(),,repr(),,zip(),,compile(),,globals(),,map(),,reversed(),,import(),,complex(),,hasattr(),,max(),,round()]
This is what you get, but we need to make some adjustment.
import tkinter as tk def create_list(inpw, outw): content = inpw.get("0.0", tk.END) content = content.splitlines() print(content) for n, i in enumerate(content): if i == "": content.pop(n) print(content) content = "mylist = [" + ",".join(content) + "]" print(content) outw.insert("0.0", content) # Widgets class Start: global text, text2 root = tk.Tk() text = tk.Text(root) text.pack(side="left") text2 = tk.Text(root) text2.pack(side="left") root.bind("<Control-b>", lambda event: create_list(text, text2)) root.mainloop()
import tkinter as tk def create_list(inpw, outw): content = inpw.get("0.0", tk.END) content = content.splitlines() print(content) for n, i in enumerate(content): print(type(i)) if i == "": content.pop(n) if type(i) == str: content[n] == f"{i}" print(content) content = "mylist = ['" + "','".join(content) + "']" print(content) outw.insert("0.0", content) # Widgets class Start: global text, text2 root = tk.Tk() text = tk.Text(root) text.pack(side="left") text2 = tk.Text(root) text2.pack(side="left") root.bind("<Control-b>", lambda event: create_list(text, text2)) root.mainloop()
Now we got also the apostrophes
With some little adjustment we could make other shortcuts to make dictionaries too.
Other examples
Subscribe to the newsletter for updates
Tkinter templates
My youtube channel
Twitter: @pythonprogrammi - python_pygame