|

Python List Comprehension Dictionary Set Comprehension and For Loops

This is a guide on Python List comprehension. List comprehension is a great way to write simple and easy to read Python codes. You can use it dictionaries and sets as well.

 

Credits to  Corey Schafer , creator of Python course materials.

lets create a list

In [115]:

 

Out[115]:

lets grab all the even numbers from that list

In [116]:

 

In [117]:

 

Out[117]:

lets use a list comperehension

In [118]:

 

In [119]:

 

Out[119]:

lets to do a square of the even numbers

In [120]:

 

Out[120]:

we can save it in a set form

In [121]:

 

Out[121]:

we can also use set in place of list in the for loop

In [122]:

 

Out[122]:

lets get letter , num pair for each letter in ‘abcd’ and number ‘0123’

In [123]:

 

In [124]:

 

Out[124]:

we can use list comprehension to achieve the same result

In [125]:

 

Out[125]:

tne parameters in the tuple should be match the parameters in the for loop . that is letter, should match letter in for loop and num
should match num in for loop. you cannot have letter in tuple or let in for number. neither can you havve num in tuple and number in
for loop

In [ ]:

We can also use list comprehension with dictionaries.

Lets create 2 lists and make a tuple of out it and then get a dictionary from the tuple

In [126]:

 

Out[126]:

Now with for loop we can get a dictionary of name being key and heroe being value

In [127]:

 

In [128]:

 

Out[128]:

we can do the same thing using comprehension. hence dictionary comprehension

In [129]:

 

In [130]:

 

Out[130]:

We can add if clauses as usual

In [131]:

 

Out[131]:
In [ ]:

A SET has UNIQUE VALUES

lets have list which has duplicates and add to a set and see how it behaves

In [132]:

 

In [133]:

 

Out[133]:

it returns only uniques values as SETS only contain unique values

We can accomplish the same thing with SET comprehension which also takes a curly bracket

In [134]:

 

Out[134]:
In [ ]:

GENERATORS. they are very similar to list comprehension but they USE PARENTHESIS ie ()
and the results can be iterated through

Lets look at one quick example

We want to get n*n for each n in nums

In [135]:

 

In [136]:

 

In [137]:

 

lets get the values from my_gen

In [143]:

 

We can do the same thing with GENERATOR

In [140]:

 

now lets iterate through my_gen2

In [141]:

 

 

Want more information like this?

Similar Posts

  • |

    Create Batch Dot Bat File to Run Your Python Script With Windows Scheduler

    After writing your script, you might want to schedule it to run periodically, let’s say, Daily, Weekly or Any Time interval you will decide Before you schedule that you want to  simply create a batch file and schedule it run with a Windows Scheduler. Here are simple steps to do that. First of all, if…

  • WindowsError: [Error 5] Access is denied: Anaconda Python Pip Installs and Upgrade

    I was trying to run a pip install ( pip install scikit-learn) and I encountered this error:

    This is the bit we are interested in ”

    ” if you look at the error it states “WindowsError” Hence the Access Denied error is due to not having the right level of privileges to carry…

  • | | | | |

    Java Python R SQL Excel Compared Similarities For Data Science and Data Analytics – The Basics

    If you have ever worked with Java, Python, R, SQL, Excel and other Languages on a varied Data Science or Data Analytics projects, you will realise that all these languages have similar syntaxes, or at least, can achieve the same objective with very similar codes.   Below is a comparison and similarities of these various tools…

  • Array Processing – Python Numpy – How to work with Arrays in Python

      Hi Guys, Thanks for all your emails. In this note, we will be looking at Array Processing in Python. This is part of lectures on Learning Python for Data Analysis and Visualization by Jose Portilla on Udemy. In [3]:

      In [4]:

      In [5]:

      In [6]:

      Out[6]:

    In [7]:

      Out[7]:

    In [8]:…

  • Python Bokeh plotting Data Exploration Visualization And Pivot Tables Analysis

    This is a quick walk through Bokeh data exploration and visualization and also python pivot_tables (credit to pbpython on the pivot_tables).Visualization Dashboard In [49]:

      In [50]:

     

    In [51]:

      Out[51]: First Name Last Name Sex Phone Fax Email Address Booking Date Departure Date Arrival Date Address1 … Number of Passengers Total Cost Currency…

Leave a Reply

Your email address will not be published. Required fields are marked *