|

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

  • |

    Pandas DataFrame and Series Alignment Python Notes

    This week we have a look at Data Alignment in Python In [1]:

      In [2]:

      Out[2]:

    In [3]:

      Out[3]:

    In [4]:

      Out[4]:

    In [6]:

      Out[6]: A B LA 0 1 GA 2 3 In [10]:

      Out[10]: A B C LA 0 1 2 NYC 3 4…

  • 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…

  • Python Pandas Groupby function agg Series GroupbyObject

    Group By FunctionThis is a quick look at Python groupby function. Very powerful and useful function. We will take a simple look at it here. Credits to Data School , creator of Python course materials. lets import sample dataset In [18]:

      In [19]:

      In [20]:

      Out[20]: country beer_servings spirit_servings wine_servings total_litres_of_pure_alcohol continent…

  • |

    Mean Squared Error – Simple Definition, Explanation and Illustration

    Having started my journey in Data Science. I came across Mean Squared Error several times and realised how important that concept is. Then I started working on project and needed to really understand what Mean Squared Error is ? So what is Mean Squared Error ? Simply,  let’s say you are building your model. And…

  • Python PyQt Gui Basics Button Window PyQt4 Pt1

    This is just some quick scrap notes on the basics of Python GUI building using PyQt. In [1]:

     

      the ‘init‘ statements runs anytime we call call the Window class we have created and it runs all the code in the Window class the _init__ method calls the super constructor and then sets…

  • | |

    Run Your Python and R Codes Online For Your Data Science and Machine Learning Projects Mini Projects For Free

    Sometimes, you would want to easily access and run your Python and R codes wherever you find yourself in the world once you have internet access. This is really handy when you have a mini-project you are working on , or you are going through an online tutorial, or you are trying to teach or…

Leave a Reply

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