• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / Mastering Lists in Python Using List Comprehensions

Mastering Lists in Python Using List Comprehensions

April 8, 2012 by cmdlinetips



Ever faced the problem of creating a unique lists in Python? or Accessing only specific elements of lists in Python? Python’s “List Comprehensions” offers you the immense power to create and use lists. Here is a brief introduction to understand and getting started on using List Comprehensions in Python.

Python’s List Comprehensions Example 1

The most simplest way in Python to create a list of integers is to use the “range” function. For example, range(10) will give you range of integers staring from 0 to 9 ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]).

However, the range function has limited use, if you want to create the list of your interest, for example if you want to create list

[0,2,4,8,16,,,]

where each element is a power of 2, it does get little bit more challenging. One approach to create the list is to use the map function in combination with lambda function as

map( lambda x: 2**x, range(10))

.

This is where Python’s List comprehension comes to help us create lists like these in a concise way. The Python List comprehensions way of creating the above list is

[2**x for x in range(10)]

You can see List Comprehensions makes it easier to create lists with no need for using map and lambda functions.

Python’s List Comprehensions Example 2

Let us see one more example of List comprehension and compare it with map/lambda function route.

Let us we have a list (range(10)) and want to get the elements of certain indices ([1,3,5])from the list.
myList = range(10)
indList = [1,3,5]

One can use the functions “map and lambda” to get the elements like

map(lambda i:myList[i], indList)

The list comprehensions way to get the elements of certain indices is

[myList[i] for i in indList]

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailGetting Started With Map, Filter, and Reduce in Python Default ThumbnailHow to Use Lambda Functions in Python? Default Thumbnail5 Examples of Using List Comprehensions in Python Default ThumbnailThree Ways to Merge (or flatten) Lists in Python

Filed Under: Python, Python List Comprehensions, Python Tips Tagged With: List Comprehensions, Python, Python Tips

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version