Taking multiple lists as arguments rather than returning them in a dict, is there a better way (python3)?

Zelnoth

I have a function where I need to return 2 lists that are created inside the function in python 3.x. Rather than returning the lists I can rather take the 2 lists as arguments and append whatever info I need in those lists. My other approach would be to return a dictionary with the 2 lists and then get the lists my using the key after the function returns. Both these methods require preparation before/after the function call, so I'm just wondering if there is a better way of returning the lists, and if not which of these 2 approaches are better.

Example code:

def method1(list1, list2):
    *do something to lists*

def method2():
    *do something to lists*
    return({'list1': list1, 'list2': list2})

get_lists_method1():
    list1 = []
    list2 = []
    method1()

get_lists_method2():
    list_dict = method1()
    list1 = list_dict['list1']
    list2 = list_dict['list2']

Edit, method using tuples:

def method3():
    *create and do something with lists*
    return (list1, list2)

get_lists_method3():
    list1, list2 = method3()
Kenny Ostrom

I would use tuple assignment. Since the two return values go together, just return (list1, list2)

# returns a tuple of two lists
def method1(list1, list2):
    list1.append(4)
    list2.append(8)
    return (list1, list2)

# the lists are result[0] and result[1]
result = method1([1, 2, 3], [2, 4, 6])
for l in result:
    print l

# more useful: tuple assignment
(list1, list2) = method1([1, 2, 3], [2, 4, 6])
print list1
print list2

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

A better way rather than using INFORMATION_SCHEMA.TABLE_CONSTRAINTS

From Dev

Returning multiple objects and using them as arguments

From Dev

Is there a better way to assign multiple arguments to multiple parameters?

From Dev

split() in python3 not returning value as expected(spliting the whole string character wise rather than space wise)

From Dev

When should Haskell functions take tuples, rather than multiple arguments?

From Dev

OCAML Taking multiple arguments from stdin and operating on them one by one

From Dev

Is there a better way to solve this than using multiple for loops?

From Dev

Initialising a large dict with unknown keys? Is there a better way than this?

From Dev

Open Python scripts in text editor rather than try to executes them

From Dev

Better way to find matches in two sorted lists than using for loops?

From Dev

Selecting from Python lists using values rather than indices

From Dev

filter lists of lists by taking out items less than a certain length - more pythonic way?

From Dev

Java - Create multiple HashMaps and fill them using a for loop - is there a better way?

From Dev

Is there a way to define roles in properties file for Spring Security plugin rather than hard coding them?

From Dev

Is there a way to have required modules on local machine rather than to get them online

From Dev

Is it better to always copy and delete, rather than move?

From Dev

Recommended way to batch stdin lines to another repeated command, like xargs but via stdin rather than arguments?

From Dev

Can you pack multiple Tkinter widgets at a time rather than packing them individually?

From Dev

Can you pack multiple Tkinter widgets at a time rather than packing them individually?

From Dev

SQLite.net Extensions loads the same entity multiple times rather than returning the same reference

From Dev

How should I implement a list in of lists in Java? Or is there a better way than a list of lists?

From Dev

TypeError: context must be a dict rather than Context

From Dev

Best way to extract data from dict with multiple dicts and lists?

From Java

How to merge the multiple lists into a dict with Python?

From Dev

How to make a dict of multiple lists in python?

From Dev

Chrome downloading pages rather than loading them

From Dev

Better way than CTE

From Dev

Is there a better way to validate multiple regex conditionals than giganic "or" statements?

From Dev

Xquery returning list of maps rather than map

Related Related

  1. 1

    A better way rather than using INFORMATION_SCHEMA.TABLE_CONSTRAINTS

  2. 2

    Returning multiple objects and using them as arguments

  3. 3

    Is there a better way to assign multiple arguments to multiple parameters?

  4. 4

    split() in python3 not returning value as expected(spliting the whole string character wise rather than space wise)

  5. 5

    When should Haskell functions take tuples, rather than multiple arguments?

  6. 6

    OCAML Taking multiple arguments from stdin and operating on them one by one

  7. 7

    Is there a better way to solve this than using multiple for loops?

  8. 8

    Initialising a large dict with unknown keys? Is there a better way than this?

  9. 9

    Open Python scripts in text editor rather than try to executes them

  10. 10

    Better way to find matches in two sorted lists than using for loops?

  11. 11

    Selecting from Python lists using values rather than indices

  12. 12

    filter lists of lists by taking out items less than a certain length - more pythonic way?

  13. 13

    Java - Create multiple HashMaps and fill them using a for loop - is there a better way?

  14. 14

    Is there a way to define roles in properties file for Spring Security plugin rather than hard coding them?

  15. 15

    Is there a way to have required modules on local machine rather than to get them online

  16. 16

    Is it better to always copy and delete, rather than move?

  17. 17

    Recommended way to batch stdin lines to another repeated command, like xargs but via stdin rather than arguments?

  18. 18

    Can you pack multiple Tkinter widgets at a time rather than packing them individually?

  19. 19

    Can you pack multiple Tkinter widgets at a time rather than packing them individually?

  20. 20

    SQLite.net Extensions loads the same entity multiple times rather than returning the same reference

  21. 21

    How should I implement a list in of lists in Java? Or is there a better way than a list of lists?

  22. 22

    TypeError: context must be a dict rather than Context

  23. 23

    Best way to extract data from dict with multiple dicts and lists?

  24. 24

    How to merge the multiple lists into a dict with Python?

  25. 25

    How to make a dict of multiple lists in python?

  26. 26

    Chrome downloading pages rather than loading them

  27. 27

    Better way than CTE

  28. 28

    Is there a better way to validate multiple regex conditionals than giganic "or" statements?

  29. 29

    Xquery returning list of maps rather than map

HotTag

Archive