List Comprehensions – Learning Python List Comprehensions w/ a non-developer

List Comprehensions – Learning Python List Comprehensions w/ a non-developer

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

#All results should look like:

#[0, 1, 2]
#[0, 1, 2]
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

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

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…

  1. the company website – very smart stuff
  2. 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.

Python for beginners – pythonforbeginners – A Puddle of Trash Water

Python for beginners – pythonforbeginners – A Puddle of Trash Water

The domain pythonforbeginners or Python for Beginners, is a massive pile of trash water ranking on Python related terms… After building a bunch of websites, and learning to rank, I realize it’s not hard to rank with poor quality. But this is different. This place actually has pop up ads trying to fish people into downloading viruses.

So, from my perspective, Python for beginners is ranking, is ranking with the water from a garbage dumpster.

Today, they are considered authority – with a BUNCH of affiliate links, virus pop ups, and google ads. Yes, fishing popups suggesting to download viruses… Google is not the best source of information for a lot of beginner related topics. The owners at Python for beginners are not actually doing any of their own work too, they are copy and pasting from other websites, and the code is poorly explained, without any logical next steps or helpful links.

The entire websites looks as if someone automated it, poorly.

Because I am an overly emotional blogger who gives a shat about people getting quality content, I’m going to stop at nothing until I beat pythonforbeginners… out of whatever keywords I decide to aim at, and I will not stop at one!

Pythonforbeginners.com is a pile of trash water

pythonforbeginners.com is a pile of trash water. They have click ads that are loaded with viruses, that trick try to trick unsuspecting people into clicking garbage, garbage faking to be Google offering updates… That’s how much of a pile of trash water pythonforbeginners truly is.

I learned list comprehensions from scratch, no mentor, no internet, on a laptop in Louisiana, down by the river!!!

pythonforbeginners is like a disgusting water vaporizer.

If you leave water in a water vaporizer for too long, the water turns into trash water. Similar to pythonforbeginners.com. You let a keyword sit without competition, it turns into trash water.

During my vacation, I ran into a bunch of trash water blogs (with limited connectivity from my dads samsung galaxy phone) and so far, most if not all examples don’t explain variations or granular details, and the past few years I basically blog about every type of technology I learn along the process, so list comprehensions is no different than some rabbit dive I’ve done, professionally, but here – it’s all new and the help docs suck, big time! HOW?

Building better tutorials, with pound for pound better content, on a website built for helping new python developers. With data analysis, competitor analysis, web scraping, and tons of google data, I’m going to find exactly how to beat this trash water website.

What will Tyler Garrett be generating with this list comprehensions wiki? First of all – the 1st Badass tutorial on List Comprehensions – with overly explained stuff… Explaining how different code examples work, based on different usages of the code, with real world help…

A list comprehensions wiki that dominates pythonforbeginners insides and outsides, your trash water stinks.

Non-trashwater solution to learning, without affiliate links or poorly written blogs.

What’s cool is the competition is low, and the yield for me to learn python, and network with people who think quality content is king, well.. yeah, let’s network fam. I spend most of my time helping others, giving back, and blogging about complex things, in simple manners.

Follow their garbage twitter here: https://twitter.com/pythonbeginners