---
title: "how to Unpack a Collection in python"  
description: "how to Unpack a Collection in python"  
author: "ICSM Computer"  
published: 2025-09-10  
updated: 2025-09-10  
canonical: https://www.mindstick.com/forum/161908/how-to-unpack-a-collection-in-python  
category: "python"  
tags: ["python-3.4"]  
reading_time: 2 minutes  

---

# how to Unpack a Collection in python

**how to Unpack a [Collection](https://www.mindstick.com/articles/1718/collections-in-java) in python with example**

## Replies

### Reply by Anubhav Sharma

In Python, **unpacking** means expanding a collection (like a `list`, `tuple`, `set`, or `dict`) into individual elements.

## 1. Unpack Lists / Tuples

```python
nums = [1, 2, 3]

a, b, c = nums
print(a, b, c)  # 1 2 3
```

### With `*` (extended unpacking)

```python
nums = [1, 2, 3, 4, 5]

first, *middle, last = nums
print(first)   # 1
print(middle)  # [2, 3, 4]
print(last)    # 5
```

## 2. Unpack in Function Calls

```python
def add(a, b, c):
    return a + b + c

nums = [1, 2, 3]
print(add(*nums))  # 6
```

## 3. Unpack Dictionaries (``)

```python
def greet(name, age):
    print(f"Hello {name}, you are {age} years old.")

person = {"name": "Alice", "age": 25}
greet(**person)
# Hello Alice, you are 25 years old.
```

## 4. Merge Collections

```python
list1 = [1, 2]
list2 = [3, 4]
merged = [*list1, *list2]
print(merged)  # [1, 2, 3, 4]

dict1 = {"a": 1}
dict2 = {"b": 2}
merged_dict = {**dict1, **dict2}
print(merged_dict)  # {'a': 1, 'b': 2}
```

## 5. Ignore Values with `_`

```python
nums = [10, 20, 30]
a, _, c = nums
print(a, c)  # 10 30
```

## Read More:

- [**Write code to Word Palindrome check**](https://www.mindstick.com/interview/34369/write-code-to-word-palindrome-check)
- [**Factorial of a number using loop**](https://www.mindstick.com/forum/161907/factorial-of-a-number-using-loop)
- [**Check if a number is Prime**](https://www.mindstick.com/forum/161906/check-if-a-number-is-prime)
- [**Reverse a string without using slicing in python**](https://www.mindstick.com/forum/161902/reverse-a-string-without-using-slicing-in-python)


---

Original Source: https://www.mindstick.com/forum/161908/how-to-unpack-a-collection-in-python

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
