Many Python developers struggle with returning multiple values from a single function. Functions in Python can return multiple values efficiently, typically as a tuple. This guide shows developers how to return multiple values python functions using simple methods like tuples, lists, and dictionaries.
Master these techniques today.
Key Takeaways
- Python automatically packs multiple return values into tuples when separated by commas in return statements.
- Developers can use tuples, lists, dictionaries, or custom objects to return multiple values from functions.
- Tuple unpacking syntax like
s, x = fun()allows assignment of multiple returned values to separate variables. - Common Lisp’s VALUES function supports up to 4,611,686,018,427,387,903 return values according to SBCL implementation.
- Multiple return values prevent TypeError errors and eliminate need for complex workarounds or global variables.
How can I return multiple values in Python?

Python functions can return multiple values from a function using a simple and elegant approach. The language packs multiple return values into a tuple automatically when values are separated by commas in the return statement.
This feature allows developers to group related results together efficiently, making code more readable and functional.
Python’s ability to return multiple values as tuples transforms how developers handle complex function outputs, making code both cleaner and more intuitive. – Alex Herrick, Web Design Booth
Functions in Python handle this process seamlessly behind the scenes. The return statement creates a tuple when multiple values appear separated by commas, even without explicit parentheses.
Developers can then unpack these values using tuple unpacking syntax like `s, x = fun()`, which yields specific values like “geeksforgeeks” and 20. This approach eliminates the need for complex data structures or multiple function calls to access different pieces of information from a single operation.
Methods for returning multiple values
Python gives programmers several smart ways to send back multiple values from a single function. These methods make code cleaner and help developers work with complex data more easily.
What are the common ways to return multiple values?
Developers face the challenge of returning multiple values from a single function in Python. Programming languages offer several approaches to handle this common requirement.
- Tuple Return Method – Functions can return values separated by commas, which Python automatically packs into a tuple. This approach works well for two or three related values that belong together.
- List Return Approach – Functions return multiple values inside a list structure, allowing modification of returned data. Lists work best when the returned values share the same data type or need updates later.
- Dictionary Return Strategy – Functions package multiple values with descriptive keys in a dictionary object. This method provides clear labels for each returned value, making code easier to read and maintain.
- Custom Class Objects – Functions create and return instances of custom classes that hold multiple attributes. Java and Kotlin use this pattern frequently for returning four or more values together.
- Generator Functions – Functions yield values one at a time through an iterator, similar to JavaScript generators. Python generators provide memory-efficient ways to return sequences of values without storing everything at once.
- Direct Multiple Returns – Languages like Go and Lua allow functions to return multiple values directly without wrapping them in containers. This approach eliminates the need for specific data structures or type constraints.
- Stack-Based Returns – Assembly and other low-level programming languages use the called stack to handle multiple return values. Stack operations can become cumbersome when passing complex data between function calls.
- Structured Binding Support – C++ allows unpacking multiple return values into individual variables using structured bindings. This feature enables clean assignment of tuple contents to separate variable names in one operation.
How do tuples help return multiple values?
Tuples pack multiple values into one neat container when a function returns data. Python automatically creates a tuple when values get separated by commas in a return statement. The function `fun()` returns a tuple containing “geeksforgeeks” and 20, which demonstrates this automatic packing behavior.
This approach keeps related results grouped together without extra code complexity.
Tuple unpacking lets programmers assign returned values to separate variables in one clean line. The syntax `s, x = fun()` splits the returned tuple into individual variables instantly.
Developers can also use the `*` operator with tuple unpacking to pass multiple values as arguments, like `user_roll(*roll_dice())`. Tuples stay immutable after creation, making them perfect for returning fixed collections of data that won’t change accidentally.
Can I use lists or dictionaries to return multiple values?
Tuples work great for returning multiple values, but lists and dictionaries offer their own advantages for specific cases. Lists are ordered collections in square brackets, e.g., `[‘geeksforgeeks’, 20]`, which makes them perfect when the returned values might change or grow over time.
A function can return a list containing different data types, and the calling code can easily modify these values later.
Dictionaries store data in key-value pairs, e.g., `{‘x’: 20, ‘str’: ‘GeeksforGeeks’}`, making them ideal when returned values need clear labels. Joshua Correos often uses dictionaries in his cybersecurity functions to return multiple security metrics with descriptive names.
This approach eliminates confusion about which value represents what data. Lists work best for ordered sequences, while dictionaries excel when each return value needs a meaningful identifier that prevents ambiguity in larger codebases.
What are the benefits of returning multiple values efficiently?
Efficient multiple value return prevents errors such as TypeError and helps promote best practices in function design. Developers can avoid the frustration of debugging code that breaks because of unexpected return formats.
Using commas to separate return values creates cleaner, more readable functions that handle complex data without extra complexity. Languages like Common Lisp demonstrate this power through the VALUES function, which can return multiple values seamlessly.
The maximum number of return values in Common Lisp reaches impressive limits, with SBCL supporting up to 4,611,686,018,427,387,903 values. This flexibility allows programmers to build robust applications that scale efficiently.
Multiple values enable seamless expansion of return data without changing code structure, similar to optional parameters. Dedicated mechanisms for multiple return values can improve compilation and runtime efficiency significantly.
Knowing the expected number of return values allows avoiding unnecessary computations that slow down programs. Creative professionals and tech enthusiasts find this approach particularly valuable when building tools that process large datasets or handle complex user interactions.
The ability to call a function and receive structured data through comma-separated values eliminates the need for complex workarounds or additional function parameters. This streamlined approach reduces development time while maintaining code quality and performance standards.
Conclusion
Python functions make returning multiple values simple and clean. Developers can choose from tuples, lists, dictionaries, or custom objects based on their project needs. Each method offers unique benefits for different coding situations.
Tuples provide the most straightforward approach for most cases. Lists work well when values might change after the function returns. Dictionaries excel when developers need named access to each return value.
These techniques help programmers write cleaner, more efficient code. Functions that return multiple values reduce the need for complex workarounds or global variables. Python’s flexibility in handling multiple return values makes it a powerful tool for any developer’s toolkit.
FAQs
1. How can Python functions return two values at once?
Python functions can return multiple values using tuples, which pack values together in a single return statement. The function creates a tuple automatically when you separate values with commas. This approach eliminates the need for complex output parameters or pointers like other programming languages require.
2. What’s the easiest way to unpack multiple return values in Python?
Use variable assignment to unpack returned values directly into separate variables. This method works because Python’s type system handles tuple unpacking automatically during assignment.
3. Can you return different data types together from a Python function?
Yes, Python functions can return mixed data types like strings, arrays, and objects in one tuple. The dynamic type system allows this flexibility without requiring a statically typed language approach.
4. How do generators help with returning multiple values efficiently?
Generators yield values one at a time using loops, which saves memory compared to returning large arrays. This control flow method works well when processing lots of data without creating huge objects in memory.
5. What are the advantages of Python’s multiple return approach over other languages?
Python avoids the complexity of pointers, null checks, and special constructors that languages like Lisp require. You don’t need getters and setters or complex parameter passing. The compiler handles everything automatically, making code cleaner and easier to read.
