How To Create An Utility For Lists In Python

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

This is the GUI, input something on the left
Now press ctrl+b
You will have your list

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

Select and copy this functions names
Now paste them into the app
Press crtl+b

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()
NOw it’s better, we get rid of the empty lines
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

Now I just can copy this into my code

With some little adjustment we could make other shortcuts to make dictionaries too.

Other examples


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.