Advanced Python Part One: List Comprehensions
Python List Comprehensions are a small tool with a large use case. They get rid of the tedious loops required for tasks such as list / string manipulation in Python in an elegant and easy way.
Python List Comprehensions
List comprehensions are Python's solution to the large loops required to do tasks which should be easy. They transform the simple task of getting all the even numbers between zero and one-thousand from: my_list = [] for index in range(1000): if index % 2 == 0: my_list.append(index) to my_list = [x for x in range(1000) if x % 2 == 0] The use for list comprehensions
List comprehensions are essentially: list_name = [expression values filter] Values - A for loop of values to check against the filter Filter - All values go through this. It is an if statement, and the expression is appended to the end of the list when it is true. Expression - Data to append onto the list if Filter is true It's important to note here that the expression could be anything. We could get the squares of all even numbers between zero and one-thousand just as easily: my_list = [x**2 for x in range(1000) if x % 2 == 0] Interesting Points
List comprehensions work on strings, and the for loop can loop through any list. The expression can be as complex as you want. The expression is what gets appended, and it can even be another list. Look at this piece of code: my_list = [[x*y for y in range(1,11) if y % 2 == 0] for x in range(1,11) if x % 2 == 0] This generates a list of lists (which is stored in my_list) where every even number between one and ten is multiplied by every other even number between one and ten. Conclusion
To practice list comprehensions you can try out Code Academy's interactive course. You can also learn more by looking at the Python Docs List Comprehensions are a very useful tool which can replace many loops if you look out for it. Cheers :)!Article Update Log
June 20 2013 - Released Article August 19 2013 - Added in Python Docs Link.Related Tutorials
Balancing Game Development and Creative Direction in Indie Production
A practical look at how indie developers can balance creative direction with hands-on game development. This article co…
My Unreal Engine Development Process: From Core Idea to Playable Build
A practical overview of my Unreal Engine development process, covering how I move from a core game idea to a playable b…
Introducing LaneGraph: The Ultimate Road Network Solution for Unity
Discover the power of LaneGraph, a lightweight and flexible lane-based navigation system for Unity. LaneGraph makes it…
Retargeting Mixamo Characters with Root Motion In Unreal Engine 5.4.
I have always found Retargeting Mixamo Characters To have Root Motion is a serious lengthy Tast, Recently I stumbled up…
How To Make A SIMPLE Main Menu In Unity
In this tutorial for unity, i go over how to make a simple main menu for unity, it's an unlisted video because i do not…
Guide to Gameplay Balance
A perspective on competitive gameplay balance, from a background of "shooter" sandbox design.
Discussion
More from Thomas Hobohm
Pre-Visualization Is Important!
Many a beginner on gamedev.net (including me) has trouble with their software in the beginning. I had no idea how to pl…
Advanced Python Part Two: Utilizing Recursion
Python is surprisingly useful for recursive algorithms, which allow for easy O(n log n) or O(log n) solutions to proble…
Discussion