Skip to content

Instantly share code, notes, and snippets.

@mccurcio
Last active May 14, 2023 23:42
Show Gist options
  • Save mccurcio/19c5fa37b43459547f532f744d4a1770 to your computer and use it in GitHub Desktop.
Save mccurcio/19c5fa37b43459547f532f744d4a1770 to your computer and use it in GitHub Desktop.
Prefer enumerate Over range
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Prefer `enumerate` Over `range`"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"trusted": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31] \n",
"\n",
"33\n",
"0b1101111111011000111100100111101\n"
]
}
],
"source": [
"from random import randint\n",
"\n",
"print(list(range(32)), '\\n')\n",
"\n",
"random_bits = 0\n",
"for i in range(32):\n",
" if randint(0, 1):\n",
" random_bits |= 1 << i\n",
"\n",
"print(len(bin(random_bits)))\n",
"print(bin(random_bits))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"trusted": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Vanilla is delicious.\n",
"Chocolate is delicious.\n",
"Pecan is delicious.\n",
"Strawberry is delicious.\n"
]
}
],
"source": [
"flavor_list = ['vanilla', 'chocolate', 'pecan', 'strawberry']\n",
"\n",
"for flavor in flavor_list:\n",
" print(f'{flavor.title()} is delicious.')"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"trusted": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1: Vanilla\n",
"2: Chocolate\n",
"3: Pecan\n",
"4: Strawberry\n"
]
}
],
"source": [
"for i in range(len(flavor_list)):\n",
" flavor = flavor_list[i]\n",
" print(f'{i + 1}: {flavor.capitalize()}')"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"trusted": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(0, 'vanilla')\n",
"(1, 'chocolate')\n"
]
}
],
"source": [
"it = enumerate(flavor_list)\n",
"print(next(it))\n",
"print(next(it))"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"trusted": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1: Vanilla\n",
"2: Chocolate\n",
"3: Pecan\n",
"4: Strawberry\n"
]
}
],
"source": [
"for i, flavor in enumerate(flavor_list):\n",
" print(f'{i + 1}: {flavor.capitalize()}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"trusted": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"gist": {
"data": {
"description": "Prefer enumerate Over range",
"public": true
},
"id": ""
},
"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.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment