-2

I'm returning 5 lists from a function in python, then I'm defining another function as such:

def excel_parser(list_titles, list_links, list_prices, num_elem_titles, num_elem_link):

I then call the function and I tried everyway, this is the only one that gives a result, but the lists get emptied, they instead contain items in them.

excel_parser(list_titles=[], list_links=[], list_prices=[], num_elem_titles=[], num_elem_link=[])

This is how I use the functions inside the second function:

    print(len(list_titles))

row = 3

for line in list_titles:
    if row <= num_elem_titles:
        Foglio_Annunci["B" + str(row)] = line
        row += 1

row = 3

for line in list_links:
    if row <= num_elem_link:
        Foglio_Annunci["C" + str(row)] = line
        row += 1

row = 3

for line in list_prices:
    if row <= num_elem_link:
        Foglio_Annunci["D" + str(row)] = line
        row += 1

row = 3
  • 2
    We need to see more of your code to understand. Please include a [minimum reproducible example](https://stackoverflow.com/help/search?q=minimum+reproducible+example) – not_speshal Sep 10 '21 at 14:32
  • 2
    Unfortunately, it is hard to know more about your problem with what is shared already. Can you please show more of your code so readers can try to replicate it on their end to see where the problem lies. I did notice, however that you seem to be setting a mutable (list) as a default argument in your method definition for `excel_parser` which carries some bad side effects. Read [this](https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument) to see why that should be avoided in Python. It might help with your issue. – idjaw Sep 10 '21 at 14:35
  • @idjaw I added the code where I use these lists inside the second function :) – Michelangelo Amoruso Manzari Sep 10 '21 at 14:37
  • 1
    Can you take a look at this and let me know if it helps you: https://docs.python-guide.org/writing/gotchas/#mutable-default-arguments – Zaid Al Shattle Sep 10 '21 at 14:40
  • @ZaidAlShattle I'm trying to get a list with all its items and use it in the second function, how can I do that? I'm studying and working, I'm doing really bad on anxiety and I read the link you gave me and didn't understand much. Sorry. – Michelangelo Amoruso Manzari Sep 10 '21 at 14:43

1 Answers1

1

In python, having lists as a parameters is almost certainly not what you are trying to do, as per here:

A new list is created once when the function is defined, and the same list is used in each successive call.

Thus, if you want to take that method, using this method instead:

def append_to(element, to=None):
if to is None:
    to = []
to.append(element)
return to

Would turn out with the correct behavior, in general anything put in the brackets for the function (defining a list for example, or calling another function), is called only once at the program startup, and is never called again.

So to conclude, instead of:

excel_parser(list_titles=[], list_links=[], list_prices=[], num_elem_titles=[], num_elem_link=[])

use:

excel_parser(list_titles=None, ...)

and inside the function for each one:

if list_titles is None:
    to = []
Zaid Al Shattle
  • 1,454
  • 1
  • 12
  • 21
  • 1
    A clever way will be to use `or` instead of `if` . `list_titles = list_titles or []`. – Shadowcoder Sep 10 '21 at 14:47
  • I did the code and rerun it, it doesn't give errors, but the excel files is exmpty, so the functions are still empty. leng(list_titles) is 0 for example. What is wrong? I added my code to your answer. – Michelangelo Amoruso Manzari Sep 10 '21 at 14:53
  • @MichelangeloAmorusoManzari the function itself looks fine with the implementation, are you sure where you declare/use your function, that you are supplying lists with data inside? Can you try to debug it and check that? Because with partial code its hard to pin-point the source of the additional issues. – Zaid Al Shattle Sep 10 '21 at 14:58
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/236971/discussion-between-michelangelo-amoruso-manzari-and-zaid-al-shattle). – Michelangelo Amoruso Manzari Sep 10 '21 at 15:02