How to convert a nested list into a one-dimensional list in Python? [duplicate]

You need to recursively loop over the list and check if an item is iterable(strings are iterable too, but skip them) or not. itertools.chain will not work for [1,[2,2,2],4] because it requires all of it’s items to be iterable, but 1 and 4 (integers) are not iterable. That’s why it worked for the second one … Read more

Overriding Tkinter “X” button control (the button that close the window) [duplicate]

It sounds as if your save window should be modal. If this is a basic save window, why are you reinventing the wheel? Tk has a tkFileDialog for this purpose. If what you want is to override the default behaviour of destroying the window, you can simply do: root.protocol(‘WM_DELETE_WINDOW’, doSomething) # root is your root … Read more

How to mute all sounds in chrome webdriver with selenium

Not sure if you can, generally for any page, do it after you have opened the page, but you can mute all the sound for the entire duration of the browser session by setting the –mute-audio switcher: from selenium import webdriver chrome_options = webdriver.ChromeOptions() chrome_options.add_argument(“–mute-audio”) driver = webdriver.Chrome(chrome_options=chrome_options) driver.get(“https://www.youtube.com/watch?v=hdw1uKiTI5c”) Or, you can mute the HTML5 … Read more

Find a link that contains a specific word using BeautifulSoup

You can do it with a simple “contains” CSS selector: soup.select(“a[href*=location]”) Or, if only one link needs to be matched, use select_one(): soup.select_one(“a[href*=location]”) And, of course, there are many other ways – for instance, you can use find_all() providing the href argument which can have a regular expression value or a function: import re soup.find_all(“a”, … Read more

eval SyntaxError: invalid syntax in python [duplicate]

For dynamic execution of statements use the exec function: >>> exec(‘y = 3’) >>> y 3 eval usage: eval(expression). The expression argument is parsed and evaluated as a Python expression. e.g.: >>> s = 3 >>> eval(‘s == 3’) True >>> eval(‘s + 1’) 4 >>> eval(‘s’) 3 >>> eval(‘str(s) + “test”‘) ‘3test’

File not found.