Building a list is a very specific building block in learning code, being a data analyst, or whatever data driven role you find yourself sitting in.
Python List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition. (from python)
What is list comprehensions? i for i
List comprehensions is the name of a way to create lists. A more technical rationale would be that list comprehensions provide a more concise way to create lists.
Before we begin, let’s discuss two simple methods to printing our list comprehensions.
print [i for i in range(3)]
Or we can use a letter, word, or letters+words. We will only use this method below.
x = [i for i in range(3)]
print x
Below, I will explain how to use list comprehensions, what list comprehensions is all about, and various ways to use list comprehensions when utilizing Python! Woohoo, getting excited, I can tell. (I can’t, I’m weird)
What is the goal with list comprehensions?
It is proposed to allow conditional construction of list literals using for and if clauses. It’s like you haven’t heard of either of these today, if so, go check out python’s website on list comprehensions. If this does make sense, you may also be interested to know the proposed solution not only lets you generate conditional lists with for and if, they would nest the same way for loops and if statements nest now.
About the tutorials below.
If you’re not entirely sure what any of the above text means, woohoo. You’re on the right track, you’re on the right blog, and if you do know the techie words above, but not entirely sure what it means in code, or in python… Check it out…
Tutorial 1, making a simple list of data in python using List Comprehensions
There are two different ways to make a list of numbers in python.
Take a look at List Comprehension and another more complicated method.
Assuming you have a basic python install and you’re able to do the 2+2 to get 4 in python.
Easy method to make a simple list in python.
x = [i for i in range(10)]
print x
#Output
## >>>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Complex method to make a simple list in python.
x2 = []
for x3 in range(10):
x2.append(x3)
print x2
#Output
## >>>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Both X and X2 generate the same output!
We will focus on the Easy method known as “List comprehension”
Notice we are using a different letter in the list comprehension code below.
When learning python, a lot of example code has “words” which can be changed to letters. Like below, I’m using a “y”… Later in this tutorial or lesson… I’m going to keep changing the “y”, which is the results of the list comprehension in python below!
y = [i for i in range(11)]
print y
You may be thinking, okay, we are increasing the numbers each time, and changing the letter in the beginning. That’s true, it’s a nice little pyramid if you run every bit of code together, which is pretty cool to know… You might be copy pasting different pieces of the code, and in this particular chunk of code, you can run the entire set of code, and not need to worry about anything! It will give you a results of our list comprehension code, which is clearly marked as “print” “firstletter”… easy enough to keep up with.. here’s a curve ball, I changed i to meow, and that works too.
z = [meow for meow in range(12)]
print z
This list comprehension code below has more than a letter, similar to the complex list generation code above, we used x2 (example 2), and similarly, we are not using a letter to describe the results of the list comprehension. For the new programmers -> Light bulb moment? You can name the “X”, “Y”, “Z” anything you damn please… If you’re following here, you should now see a pattern, and easy pattern to generate simple lists with a range of #’s. Also, you might be seeing a little bit of math. How exciting! You’re doing math, in python!
tyler = [i for i in range(14-1)]
print tyler
In the list comprehension below, I’m simply demonstrating addition… On the range() portion of the code.
tyler2 = [DOG for DOG in range(12+2)]
print tyler2
More math, yes division, multiplication, subtraction and addition work in python, pretty well too..
I tested, it’s accurate.
tyler3 = [i for i in range(14/2+7+1)]
print tyler3
The next few tutorials on list comprehensions will be right up your alley, if you understand the techie mumbo jumbo this will be a good refresh and a hilarious chunk of code you can likely help optimize, and I will gladly add anyone in the tutorial blog posts – and link to whatever website, github profile, etc, and help you rank better. As a novice at all things python, I’m excited to get to know you more as you take the journey I once took. I’m excited to get to know people who think this is total trash too. I need that kind of feedback. I’m learning every day, coding is hard, and I only know what I know, and that means if someone could show me all the ways the code works, I will master the code, otherwise I’m MR. Trial and error. so longest story short, you’re here to learn, me too, so let’s get started by learning everything there is to know about list comprehensions.
Python list comprehensions is as necessary as eating this shaved ice desert.
List comprehensions are powerful but poorly explained by the current top two ranking websites. That’s because they are…
the company website – very smart stuff
some random blog with affiliate links and viruses, popup ads..
I’m excited to show you a proper list comprehensions tutorial, without a single pop up, google ad, or affiliate link.
Pythonforbeginners sold their blog off for marginal return. Popup ads in 2018…
Making Lists in Python – List Comprehensions
Some may call this an “i for i” + “python” on google!
Why is Tyler Garrett rebuilding the wiki on List comprehensions?
The current content when googling i for i python or list comprehensions, is currently plundered with garbage water.
Automate generating a list of values using python 2.X
List comprehensions 101…
If you are new, eager to learn quickly, want to learn without having to code, need lots of examples… Looking for a lot of sample code, that tests a lot of different variations of list comprehensions, or maybe… You looked online, didn’t find anything decent, a little upset trying to learn python, need a better guide… Maybe it’s just me.
Cool, that’s why I write, to replace the TRASH WATER blogs online. No monetizing, no click ads, just solutions.
Ready to start learning python? You may think installing python 3.7 is the right step, I recommend starting with python 2.7 and upgrade if necessary. If you insist, here’s how to get python 3.7 on windows 10 pc.
After this blog you will..
become yoda
install python 3.7 on windows 10
use python 3.7
links to tutorials and beginner info
Installing python on windows PC!
If you’re running Windows: the most stable Windows downloads are available from the Python for Windows page. Screenshot walkthrough, step by step, on bottom of recommendations portion, built for new people learning to install python on windows PC for the first time.
Begin the download and follow screenshot instructions below.
If you get stuck or lost — you can ask me for helpIf you’re a first timer, and you just think python 3.7 install on windows 10 is the next best step…
Please, read through and learn from my awful mistakes.
Need to learn more about python install and my recommendations?
I made a bunch of stupid mistakes learning python because there’s lame ass websites ranking and they don’t really have very good content. Like Python For Beginners — spams malicious popup ads, I blog about it in that link.
Before we begin, I want to mention…
I also write about uninstalling python 3.7 because my project ended up requiring 2.7, which is available natively if you have a macbook!
Quick hint, keep your installer! You likely don’t need python 3.7 today if you’re new, and you likely will be better off using an older version, which will have more documentation, college courses, smarter people than me… Blogging about these older versions, for over a decade… But if you insist. The answer to installing python 3.7 is one header down.
If you’re not already using pycharm, I strongly consider you find a smart python writing tool, if you really need python on your local computer, I’m going to recommend stop…
Downloading and installing on your local machine isn’t necessary and could cause issues if you already have a version installed and you’re not familiar with the basics… I understand because I, Tyler Garrett, also sucked a lot at this portion and felt obliged to share the solution and the journey.
Screenshots from installing python on windows PC Python Release 3.7.0
Open your executable.
Or if you’re not that techie, navigate to your download folder…
Or search your computer for python-3.7.0-webinstall.exe
Testing to see if python 3.7.0 installed on your windows machine
A black screen opens and you will see something like:
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
>>>
You will be able to type in front of >>>
Type 2+2+1
Congrats, you just used Python to do basic aggregation.
“next steps: check out setting up pip on python 3.7 below”
“Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms.” Source.
“The Python interpreter and the extensive standard library are freely available in source or binary form for all major platforms from the Python Web site, https://www.python.org/, and may be freely distributed. The same site also contains distributions of and pointers to many free third party Python modules, programs and tools, and additional documentation.” Source.
So if you’re not sure what you should download, and you’re not looking to use a PC with python, go look here to begin.
Here’s a quick deep dive into python.
By the way, the language is named after the BBC show “Monty Python’s Flying Circus” and has nothing to do with reptiles. Making references to Monty Python skits in documentation is not only allowed, it is encouraged! Source.
Python allows you to split your program into modules that can be reused in other Python programs. It comes with a large collection of standard modules that you can use as the basis of your programs — or as examples to start learning to program in Python.
If you do much work on computers, eventually you find that there’s some task you’d like to automate.
Alright, cool story, have fun.
So…
IfI’m not building analytics apps at work, or helping musicians download free loops and samples.. I enjoy writing terribly about Python, or other tech tutorials.
Previous version of the python 3.7 blog, back when i typed more than coded.
Install python, or do my how to install python 3.7 windows 10. This can be accomplished using the python installer at python.org. Setting up Python 3.7 on windows 10, the non-developer tutorial for those who don’t want to figure out every little aspect, and prefer having someone teach them the path to success. Read more of my python struggles here.
Python 3.7 is rad — I wanted to originally use it but I realized I was not able to shorten links with the tinyurl library, so I reverted back to a version that allowed me to use the tinyurl library (python 2.7).
After you download the latest version of python, like python 3.7, you have a little bit of a journey ahead of you. Let’s begin.
Python requires a type of installer to gain access to complex libraries written in god only knows… The goal is you don’t need to write these complex libraries from scratch, you can call them up, with non-complex code, and get away with practical murder from an automation perspective.
I’m excited about the fact that I can build what I want in a few lines of code — and so far it makes sense. But to get to the point of making your apps, we need some foundation, a means of downloading, there are a few methods, let’s start with the pip method first.
Seriously, 2.7 is easier for newbs. Go there first. I’m learning on 2.7 and after writing this blog for 3.7, I realized I wanted to be on a later version to use fun stuff like building short links with tinyurl in python.
Setting up Python 3.7 Pip on Windows 10
Next steps are rather easy, you can follow along, and setup your python pip installer — without breaking a sweat, on windows 10.
Right click the website and save this to your desktop.
Double click the get-pip.py file, and wait for the installer to complete. That’s all, now pip works. Check it by following this next step.
Testing PIP is working with your new python 3.7 install on windows 10
Open your cmd terminal thingy, hit the windows key, type cmd, hit enter. Your terminal should automatically popup.
Type “pip” in CMD to learn more about pip
/!\ Before do anything, install or upgrade the Setuptools Python package. It contain compatibility improvements and add automatic use of compiler
pip install — upgrade setuptools
install microsoft visual C++ 14 — setting up visual studio build tools 2017
Installing microsoft visual c++ 14 seems to be necessary when setting up lxml, a type of parsing tool that allows you to quickly parse XML and HTML, which we will want to build our web parsing, or scraping tools.
Will know if our pip install lxml errors clear up after this restart (oh yeah and restart is necessary here).
Getting this pip installer thing going takes a little bit, but luckily I’ve consolidated it here lol.
The install takes a little bit of time too.
Here’s what you need to check out to knock out the visual studio portion.
Run the installer, don’t select anything other than default settings, and click install. It takes a little bit, and then you need to restart.
Once we get back from the restart, we will do the following pip installs.
pip install lxml (breaks with out c++14.0.)
pip install requests (ran this early, seems to install no hiccups.)
pip install beautifulsoup4 (ran this early, seems to install no hiccups.)
Will report back and iron out these posts more later.
If you have trouble with your pip install on python2 vs python3, or something along those lines, check out this next blog… sometimes you can get ahead, and there are a few things to ensure you have completed, like the above mumble jumble.
Update, started using pycharm and rather enjoying it. It’s a type of code editing “easy mode” application that you might want someone to walk you through a few times. Or google a youtube video. I figured it out without googling or looking anything up, which means you can fly past me with the right mentor.