Test your Programming knowledge

50% users failed to get the best score. It's your turn to test now.
Questions curated by the expert mentors at codzify.com

Start Quiz

Article

Python List Comprehension

2 min 24 sec read

Python lists are similar to arrays declared in other languages. But the most powerful thing is that list need not be always homogeneous. A single list can contain strings, integers, as well as objects. The list is a collection which is ordered and changeable.

The basic example of List

# Create a list ls = [5, 7, 25] # Prints the type of the variable print(type(ls)) # Print the contents of the list print(ls) # elementwise indexing in the list print(ls[0], ls[1], ls[2]) # Append an element at the end of the list ls.append(45) print(ls) # Remove and return the last element of the list x = ls.pop() print(x, ls)

Example 2

# Unlike arrays, list can contain elements of different dataypes ls = ["codzify", ".com", 23] # Print the contents of the list print(ls) # indexing in list print(ls[0], ls[1], ls[2]) # Append an element at the end of the list ls.append("hello world") print(ls) # Remove and return the last element of the list x = ls.pop() print(x, ls)

Slicing in Python

Other than accessing list elements one at a time, we can access the sublists python slice. Comments in the given example will help you to understand the concepts properly. Try to execute the code to see the output.

ls = list(range(6)) print(ls) # Negative Indexing is also possible, it will print from the end of the lis print(ls[-1], ls[-2], ls[-3]) # Get a sublist starting from index 1 to 3 (last index is exclusive) print(ls[1:4]) # Get a sublist starting from index 2 to the end print(ls[2:]) # Get a sublist from start to index 3 (last index is exclusive) print(ls[:4]) # Get the whole list print(ls[:]) # Assign a new sublist to a slice ls[2:4] = ["codzify", "python"] print(ls)

Looping through the list

This example illustrates you how to loop through the elements of list in python.

languages = ["C", "C++", "Python", "Java", "Swift"] for x in languages: # Some Code print(x)

List Comprehensions

Python list Comprehensions are used whenever you want to frequently transform one type of data into another.

To understand the concept we will demonstrate to you using the old traditional method to write programs and what you can do using python list comprehension.

Example 1:

Using an old traditional way find the square of numbers in python.

# Traditional way nums = list(range(1,5)) squares = [] for x in nums: squares.append(x ** 2) print(squares)

Using List Comprehension find the square of numbers in python.

# creates a list from range 1 to 5 nums = list(range(1,5)) squares = [x ** 2 for x in nums] print(squares)

Example 2:

Find the even and odd numbers from 1 to 10 using old traditional way

# creates a list from range 1 to 10 nums = list(range(10)) even = [] odd = [] for i in nums: if i%2 == 0: even.append(i) else: odd.append(i) print("Even Numbers: ", even) print("Odd Numbers : ", odd)

Find the even and odd numbers from 1 to 10 using List Comprehension

nums = list(range(10)) even = [x for x in nums if x%2 == 0] odd = [x for x in nums if x%2 != 0] print("Even Numbers: ", even) print("Odd Numbers : ", odd)

Recommended Articles

  1. Python Numbers
  2. Python Strings
  3. Python Loops
  4. Python Sets
  5. Python Tuple

Did you found this article helpful?

Try to execute what you have learnt

Easy to use online data structure compiler where you can execute the programs in your favourite programming language.
(C, C++, Python)

Open Compiler

HTML, CSS and Javascript Real time Web Editor

Execute your HTML, CSS and javascript code in real time with the web editor
(HTML, CSS, Bootstrap, Javascript)

Open Web Editor