• 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 / 5 Examples of Using List Comprehensions in Python

5 Examples of Using List Comprehensions in Python

May 1, 2017 by cmdlinetips

List Comprehensions in Python are awesome. List Comprehensions lets your create lists without actually writing a for loop. A while back wrote a post on some examples of using List Comprehensions in Python. Here are 5 more examples of List Comprehensions, that will come in extremely handy whenever you deal with lists in Python.

1. How to Convert a list of integers to a list of strings?

Let us say we have a list of integers like

>my_list = [0,1,2,3,4,5]

and we can use List Comprehensions

>[str(x) for x in my_list]

to get a list of strings

['0', '1', '2', '3', '4', '5']

2. How to get tuples from two lists with List Comprehensions?

Let us say we have two lists “my_list1” and “my_list2”

>my_list1 =[0,1]
>my_list1 =['zero','one']

We can use List comprehensions to loop through each list and write out tuples of the elements.

>[(x,y) for x in my_list1 for y in my_list2]

and get the list of tuples.

[(0, 'zero'), (0, 'one'), (1, 'zero'), (1, 'one')]

3. How to Get Index of Each Element of List with enumerate and List Comprehensions

Often you may need to have access to get index of each element in a list. We can use List comprehensions and enumerate in python to get index of each element in a list. Let us say we have list

>my_list= ['a','b','c','d','e']

Python’s built-in function ‘enumerate’ lets us to loop over a list and get both the index and the value of each item in the list. We can combine enumerate with List comprehensions to get a index and element tuple.

>[(i,j) for (i,j) in enumerate(my_list)]

we get

[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e')]

4. How to Use if Statement in List Comprehensions?

In the earlier example, we saw how to use enumerate and list comprehensions to index of each element in a list. Suppose, if we want index of a specific element, we can use if statement in list comprehension to check for specific element and get its (or their) index. For example, to get index of a specific element ‘c’ in a list, we can use

>[i for (i,j) in enumerate(my_list) if j=='c']

we will get

[2]

5. How to Use if-else statement in List Comprehensions?

Let us say we have a list of integers.

>my_list = range(10)
>my_list
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

We will use if-else condition within List Comprehension to get a list of strings telling us whether each element is an odd or even number.

>['even' if i%2==0 else 'odd' for i in my_list]
['even', 'odd', 'even', 'odd', 'even', 'odd', 'even', 'odd', 'even', 'odd']


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 ThumbnailMastering Lists in Python Using List Comprehensions Default Thumbnail5 Examples Using Dict Comprehension in Python Default ThumbnailHow to Compute Executing Time in Python? Default ThumbnailGetting Started With Map, Filter, and Reduce in Python

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

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