---
title: 'Generating multiple lists from a list of tuples'
url: 'https://kb.hills-family.uk/blog/generating-multiple-lists-from-a-list-of-tuples'
markdown: 'https://kb.hills-family.uk/blog/generating-multiple-lists-from-a-list-of-tuples.md'
date: '2021-05-13'
description: "I was once working with some data in Python that would generate a list of tuples, for example: myList = [('ab',1), ('cd', 2), ('ef', 3)] I wanted to obtain two lists from this: the first list containing the first element of each tuple and the second list containing the second element of each tuple. The zip function goes part of the way there: >>> a, b = zip(*y) >>> a ('ab', 'cd', 'ef') >>>"
taxonomy:
  tag:
    - Python
---

# Generating multiple lists from a list of tuples

[ Home ](https://kb.hills-family.uk/)     [ Blog ](https://kb.hills-family.uk/blog)      Generating multiple lists from a list of tuples    

   13 *May*   
#### [Generating multiple lists from a list of tuples](https://kb.hills-family.uk/blog/generating-multiple-lists-from-a-list-of-tuples)

  [Python](https://kb.hills-family.uk/blog/tag:Python)  

I was once working with some data in Python that would generate a list of tuples, for example:

```python
myList = [('ab',1), ('cd', 2), ('ef', 3)]
```

I wanted to obtain two lists from this: the first list containing the first element of each tuple and the second list containing the second element of each tuple. The `zip` function goes part of the way there:

```python
>>> a, b = zip(*y)
>>> a
('ab', 'cd', 'ef')
>>> b
(1, 2, 3)
```

Effectively, `zip` would produce two tuples. Individually converting these to a `list` (e.g. `list(a)` and `list(b)`) would work, but would not be feasible if the tuples had many more entries. This is where the `map` function comes in:

```python
>>> a, b = map(list, zip(*y))
>>> a
['ab', 'cd', 'ef']
>>> b
[1, 2, 3]
```

At this point, I also found out that Python’s syntax also supports pointers directly to list, such that:

```python
a, b = map(list, zip(*[('ab',1), ('cd', 2), ('ef', 3)]))
```

works! I forget that Python syntax can differ from C!

 [ Next Post](https://kb.hills-family.uk/blog/duplicate-label-names-in-microsoft-dynamics-ax) [Previous Post ](https://kb.hills-family.uk/blog/hopping-with-ssh)

#### Knowledge Base

Tips, tricks and how-to guides on various topics.

#### Popular Tags

  [Linux](https://kb.hills-family.uk/blog/tag:Linux) [Dynamics AX](https://kb.hills-family.uk/blog/tag:Dynamics%20AX) [D365 Finance and Operations](https://kb.hills-family.uk/blog/tag:D365%20Finance%20and%20Operations) [Network](https://kb.hills-family.uk/blog/tag:Network) [Data Management Framework](https://kb.hills-family.uk/blog/tag:Data%20Management%20Framework) [Logic Apps](https://kb.hills-family.uk/blog/tag:Logic%20Apps) [JavaScript](https://kb.hills-family.uk/blog/tag:JavaScript) [netcat](https://kb.hills-family.uk/blog/tag:netcat) [SSH](https://kb.hills-family.uk/blog/tag:SSH) [PowerShell](https://kb.hills-family.uk/blog/tag:PowerShell) [Virtual Machines](https://kb.hills-family.uk/blog/tag:Virtual%20Machines) [VMware](https://kb.hills-family.uk/blog/tag:VMware) [SQL Server](https://kb.hills-family.uk/blog/tag:SQL%20Server) [Wireshark](https://kb.hills-family.uk/blog/tag:Wireshark) [Python](https://kb.hills-family.uk/blog/tag:Python) [Licensing](https://kb.hills-family.uk/blog/tag:Licensing) [Ubuntu](https://kb.hills-family.uk/blog/tag:Ubuntu)

---

## Navigation

- Parent: [Blog](https://kb.hills-family.uk/blog.md)
- Previous: [Duplicate label names in Microsoft Dynamics AX](https://kb.hills-family.uk/blog/duplicate-label-names-in-microsoft-dynamics-ax.md)
- Next: [Hopping with SSH](https://kb.hills-family.uk/blog/hopping-with-ssh.md)
