Is it possible to manipulate lists of results from several functions into one new list?

Question:

I want to make a new list including the results from all three def functions and I do not know how. I am expecting to add all three results from the sites into a new list and then find the lowest price from the list, the highest price, maybe average price(but these i think I can do them by myself).

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
options.add_argument("start-maximized")
webdriver_service = Service('C:webdriverschromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 30)

 def bestauto(driver):
    driver.get("https://www.bestauto.ro/auto/?q=volvo+xc90+2020&pag=1&carregistrationdate=2020-2021")
    wait.until(EC.element_to_be_clickable((By.ID, 'cookieAccept'))).click()
    block = driver.find_elements(By.XPATH, ".//div[@class='listing-data']")

    result_bestauto = []
    for i in block:
        prices = i.find_element(By.XPATH, ".//strong[@class='price maincolor']").text
        price = prices.strip(' EUR').replace(' ', '.')
        result_bestauto.append(price)
    return result_bestauto

def autovit(driver):
    driver.execute_script("window.open('');")
    driver.switch_to.window(driver.window_handles[1])
    driver.get("https://www.autovit.ro/autoturisme/volvo/xc-90/seg-suv/de-la-2020?search%5Bfilter_enum_generation%5D=gen-ii-2015&search%5Bfilter_float_year%3Ato%5D=2021")
    wait.until(EC.element_to_be_clickable((By.ID, 'onetrust-accept-btn-handler'))).click()
    wait.until(EC.element_to_be_clickable((By.XPATH, ".//div[@class='ooa-z4wqde eg0t0xb1']"))).click()
    cars = driver.find_elements(By.XPATH, ".//article[@class='ooa-1txt27o e1b25f6f0']")

    result_autovit = []
    for y in cars:
            car_cost = y.find_element(By.XPATH, ".//span[@class='ooa-1bmnxg7 e1b25f6f11']").text
            cost = car_cost.strip("EUR ").replace(' ', '.')
            au = float(cost)
            au_dec = "{:.3f}".format(au)
            result_autovit.append(au_dec)

    if len(cars) < 6:
        promoted_car = driver.find_element(By.XPATH, ".//article[@class='ooa-1wl9plw e1b25f6f0']")
        pretulMasinii = promoted_car.find_element(By.XPATH, ".//span[@class='ooa-1bmnxg7 e1b25f6f11']").text
        pret = pretulMasinii.strip("EUR ").replace(' ', '.')
        a = float(pret)
        result_autovit.append(a)

    return result_autovit #= class type

def anuntul_ro(driver):
    driver.execute_script("window.open('');")
    driver.switch_to.window(driver.window_handles[2])
    driver.get("https://www.anuntul.ro/anunturi-auto-moto/autoturisme/?search%5Bsumar%5D%5BrubricaId%5D=5&search%5Bsumar%5D%5BsubrubricaId%5D=30&search%5Bfields%5D%5B0%5D%5Bfields%5D%5B0%5D%5Bvalue%5D=Volvo&search%5Bfields%5D%5B0%5D%5Bfields%5D%5B0%5D%5Bfields%5D%5B1%5D%5Bvalue%5D%5B%5D=XC90&search%5Bfields%5D%5B0%5D%5Bfields%5D%5B2%5D%5Bvalue%5D%5Bmin%5D=&search%5Bfields%5D%5B0%5D%5Bfields%5D%5B2%5D%5Bvalue%5D%5Bmax%5D=&search%5Bfields%5D%5B0%5D%5Bfields%5D%5B3%5D%5Bvalue%5D%5Bmin%5D=2017&search%5Bfields%5D%5B0%5D%5Bfields%5D%5B3%5D%5Bvalue%5D%5Bmax%5D=&search%5Bfields%5D%5B0%5D%5Bfields%5D%5B5%5D%5Bvalue%5D%5Bmin%5D=&search%5Bfields%5D%5B0%5D%5Bfields%5D%5B5%5D%5Bvalue%5D%5Bmax%5D=&search%5Bfields%5D%5B0%5D%5Bfields%5D%5B7%5D%5Bvalue%5D%5Bmin%5D=&search%5Bfields%5D%5B0%5D%5Bfields%5D%5B7%5D%5Bvalue%5D%5Bmax%5D=&search%5BcautareId%5D=&search%5Bquery%5D=&search%5Blat%5D=&search%5Blng%5D=&search%5Bsortf%5D=valabilitate.sort&search%5Bsorts%5D=-1&search%5Bpage%5D=&search%5Bowner%5D=")
    time.sleep(3)
    wait.until(EC.element_to_be_clickable((By.ID, 'acordCookies'))).click()
    masini = driver.find_elements(By.XPATH, ".//div[@class='clearfix line-btw anunt-row i-cb i-pr anunt-w  ']")
    result_anuntul_ro = []
    for x in masini:
        pret_masina = x.find_element(By.XPATH, ".//div[@class='float-right price-list i-fr']").text
        valuare = pret_masina.strip(" €").replace(' ', '.')
        an = float(valuare)
        an_dec = "{:.3f}".format(an)
        result_anuntul_ro.append(an_dec)
    return result_anuntul_ro

if __name__ == "__main__":
    result1 = bestauto(driver)
    result2 = autovit(driver)
    result3 = anuntul_ro(driver)
    all_cars = result1 + result2 + result3
Asked By: Diana Stroescu

||

Answers:

You can concatenate the 3 lists into 1 list as following:

import itertools
all_cars = list(itertools.chain(result1, result2, result3))

now you will be able to find minimal and maximal values as following:

minimal_price = min(all_cars)
maximal_price = max(all_cars)

As about average value – there are several ways to get it, I’d prefer this:

L = [float(n) for n in all_cars if n]
average_price = sum(L)/len(L) if L else '-'
Answered By: Prophet