Skip to content

Instantly share code, notes, and snippets.

@pdbartsch
Created March 23, 2023 15:57
Show Gist options
  • Save pdbartsch/72ef7d9f6433f8dc20e50721255129d8 to your computer and use it in GitHub Desktop.
Save pdbartsch/72ef7d9f6433f8dc20e50721255129d8 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "623c5b3f",
"metadata": {},
"source": [
"# zip() in Python"
]
},
{
"cell_type": "markdown",
"id": "83507115",
"metadata": {},
"source": [
"### Archived copy if you'd like to follow along:\n",
"- [On NBViewer]()\n",
"- [as Gist]()"
]
},
{
"cell_type": "markdown",
"id": "11fd0045",
"metadata": {},
"source": [
"- The zip() function is used to combine multiple iterables (lists, tuples, dictionaries, sets, strings, etc.) into a single iterable of tuples (x,y,z). \n",
"\n",
"- Iterables are just collections of data that can be itterated through one by one. \n",
"\n",
"- Each element of the zip object is a tuple containing the corresponding elements from the original iterables."
]
},
{
"cell_type": "markdown",
"id": "c9c05186",
"metadata": {},
"source": [
"### zip() is a built-in function\n",
"- nothing to install\n",
"- changed significantly between python 2 and 3. I'll be using Python 3."
]
},
{
"cell_type": "markdown",
"id": "2fb2184d",
"metadata": {},
"source": [
"### what other built-in functions are there?\n",
"- also check that zip() is really a built-in"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9d0241e2",
"metadata": {},
"outputs": [],
"source": [
"dir(__builtins__) # see zip at the end of the list"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4f079a73",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "4ef5bbbd",
"metadata": {},
"source": [
"### Reminder: Help is always nearby"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9ff3e2c8",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# help() #starts an interactive help session\n",
"help(help)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2fb23053",
"metadata": {},
"outputs": [],
"source": [
"help(zip)"
]
},
{
"cell_type": "markdown",
"id": "512fd1c9",
"metadata": {},
"source": [
"## Okay, on with zip()\n",
"### Simplest example:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "38778d27",
"metadata": {},
"outputs": [],
"source": [
"x = [1,2,3]\n",
"y = ['a','b','c']\n",
"z = zip(x,y)\n",
"print(z)\n",
"list(z) # as a list of tuples"
]
},
{
"cell_type": "markdown",
"id": "de31b280",
"metadata": {},
"source": [
"### zip can handle any number of iterables\n",
"##### example with three:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c9927ce3",
"metadata": {},
"outputs": [],
"source": [
"numbers = [1,2,3]\n",
"str_numbers = ['One','Two','Three']\n",
"roman_numbers = ['I','II','III']\n",
"result = zip(numbers, str_numbers, roman_numbers)\n",
"print(result) # zip object, memory location\n",
"list(result) # as a list of tuples"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7ea4f277",
"metadata": {},
"outputs": [],
"source": [
"# without zip, you could accomplish a similar task with a for loop,\n",
"# appending a tuple for each combination to the result list\n",
"\n",
"numbers = [1, 2, 3]\n",
"str_numbers = ['One', 'Two', 'Three']\n",
"roman_numbers = ['I', 'II', 'III']\n",
"\n",
"result = []\n",
"for i in range(len(numbers)):\n",
" result.append((numbers[i], str_numbers[i], roman_numbers[i]))\n",
"\n",
"print(result)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aa8cc159",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "8ad976e0",
"metadata": {},
"source": [
"---\n",
"### Can be used to zip different data types together:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3b494219",
"metadata": {},
"outputs": [],
"source": [
"# notice mix of data types in both the tuple and the list\n",
"# tuples and lists are examples of two different types of sequence or ordered collections in Python\n",
"my_list = ['zero',1,'two',3,4,5]\n",
"my_tuple = ('zero',1,'two',3,4,5)\n",
"\n",
"my_zip = zip(my_list,my_tuple) # interesting...that worked\n",
"\n",
"list(my_zip)\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "8b88cc6f",
"metadata": {},
"source": [
"---\n",
"### Works on iterables of different lengths too, but will only go as far as the shortest one."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "02d7e290",
"metadata": {},
"outputs": [],
"source": [
"numbers = [1,2,3,4,5,6,7,8,9,10]\n",
"str_numbers = ['One','Two','Three']\n",
"result = zip(numbers, str_numbers) \n",
"list(result)"
]
},
{
"cell_type": "markdown",
"id": "9bee0e41",
"metadata": {},
"source": [
"---\n",
"### ok, but why do I have to turn a zip into a list before I can see the actual data that I'm after?\n",
"\n",
"good [answer here](https://stackoverflow.com/a/19777643/747748)\n",
"\n",
"Python 2 did return lists as one might expect. The change in Python 3, to product these elements only on demand rather than expanding list into memory is more efficient in typical use cases"
]
},
{
"cell_type": "markdown",
"id": "7f5f0b39",
"metadata": {},
"source": [
"---\n",
"### next() method:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eccd8f92",
"metadata": {},
"outputs": [],
"source": [
"numbers = [1, 2, 3]\n",
"letters = ['a', 'b', 'c']\n",
"zipped = zip(numbers, letters)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ebd478c0",
"metadata": {},
"outputs": [],
"source": [
"# interesting. let's mess around with this a bit:\n",
"#list(zipped)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "020badb1",
"metadata": {},
"outputs": [],
"source": [
"print(next(zipped))\n",
"# Note that once you have iterated through all of the elements of a zip object using the \"next()\" function, \n",
"# you cannot iterate through it again using the same zip object. \n",
"# Instead, you will need to create a new zip object if you want to iterate over the elements again."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dfb87662",
"metadata": {},
"outputs": [],
"source": [
"list(zipped)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "67f31ede",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "96dc6974",
"metadata": {},
"outputs": [],
"source": [
"# how do we get to the first tuple of z?\n",
"first_element = next(zipped)[0]\n",
"print(first_element)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "37b727b3",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "6d98273b",
"metadata": {},
"source": [
"---\n",
"### How many elements in this zip I'm working with?"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "65d07b0b",
"metadata": {},
"outputs": [],
"source": [
"print(len(zipped)) # intentional error, remember that a zip object generates values as needed"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "780c1288",
"metadata": {},
"outputs": [],
"source": [
"count = 0\n",
"for i, val in enumerate(zipped):\n",
" count += 1\n",
"\n",
"print(count) # Output: 3"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c8f33842",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "ec5159d9",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "a10f978f",
"metadata": {},
"source": [
"---\n",
"### We've been printing the zip object cast as a list of tuples\n",
"#### Now, we'll cast as a dictionary"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5dda5ffa",
"metadata": {},
"outputs": [],
"source": [
"## first example\n",
"x = [1,2,3]\n",
"y = ['a','b','c']\n",
"z = zip(x,y)\n",
"z_dict = dict(z) #(key,value)\n",
"z_dict"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ee88a954",
"metadata": {},
"outputs": [],
"source": [
"## second example\n",
"keys = ['name', 'age', 'city']\n",
"values = ['Alice', 30, 'New York']\n",
"my_dict = dict(zip(keys, values))\n",
"my_dict"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "307081d1",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "9ddecb4f",
"metadata": {},
"source": [
"### [Continue learning about zip()](https://betterprogramming.pub/7-useful-ways-to-use-the-zip-function-in-python-2b936414805e)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9ea8771a",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "d02e4b90",
"metadata": {},
"source": [
"### Slightly Off-Topic Reminder:\n",
"- lists are mutable (they can be changed)\n",
"- tuples are immutable (can't be changed)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e432c803",
"metadata": {},
"outputs": [],
"source": [
"# notice mix of data types in both\n",
"my_list = ['zero',1,'two',3,4,5]\n",
"my_tuple = ('zero',1,'two',3,4,5)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "965757ea",
"metadata": {},
"outputs": [],
"source": [
"# see current values at 5th position ... start with zero\n",
"my_list[4] #discuss slicing in a future meeting?"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f0d3bad3",
"metadata": {},
"outputs": [],
"source": [
"my_tuple[4]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "917ad6af",
"metadata": {},
"outputs": [],
"source": [
"my_list[4] = 'four'"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3dd8857b",
"metadata": {},
"outputs": [],
"source": [
"my_list"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "515223e3",
"metadata": {},
"outputs": [],
"source": [
"my_tuple[4] = 'four'"
]
},
{
"cell_type": "markdown",
"id": "61e9a41a",
"metadata": {},
"source": [
"code that uses tuples is slightly faster than code that uses lists"
]
},
{
"cell_type": "markdown",
"id": "bf282919",
"metadata": {},
"source": [
"Although you can't modify an item in a tuple you can replace the whole thing:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a660d847",
"metadata": {},
"outputs": [],
"source": [
"my_tuple = ('zero', 1, 'two', 3, 'four', 5)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "964e9c80",
"metadata": {},
"outputs": [],
"source": [
"my_tuple"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d00061fb",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "4dc19dbd",
"metadata": {},
"source": [
"Future topic ideas:\n",
"- [f-strings](https://realpython.com/python-f-strings/#f-strings-a-new-and-improved-way-to-format-strings-in-python)\n",
"- dictionary comprehension\n",
"- data types including dictionaries, tuples, sets, strings, etc & how to create modify and access a value\n",
"- the `yield` keyword\n",
"- more on generators and iterables. \n",
" "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.2"
},
"vscode": {
"interpreter": {
"hash": "1870011e98754ee692ec9964d752544743c50a27e7aeb289b0fa14754112bbe4"
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment