How to use for loop inside a list in python
In this article, we will understand using the for loop inside the list in python programming. Let’s understand this concept step by step.
Let’s say we have a list “squares_values = []” and we want to append some data. let’s say wish to add 10 square values to it. so using the code we can write like this.
squares_values = []
for i in range(10):
squares_values.append(i*i)
Output - [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
squares_values = [i*i for i in range(10)]
Output - [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
so here we are writing the same above three lines in one line and the output is the same. so how to use for loop inside a list. so the syntax goes like this
List_Name = [value [for loop condition]]
Even if I can say there is not any syntax over there because we can apply as much as conditions as we want or based on the condition. let’s say we want the use if condition inside a for loop and that for loop is used for appending the data in the list. so instead of writing the code like this.
squares_values = []
for i in range(10):
if i%2 == 0:
squares_values.append(i*i)
we can simply write the code like this.
squares_values = [i*i for i in range(10) if i%2 == 0]
so there is nothing different between both codes. this feature is similar to like list comprehension.
hello!,I like your writing very much! share we communicate more about your article on AOL? I need a specialist on this area to solve my problem. Maybe that’s you! Looking forward to see you.
Hello my friend! I want to say that this article is amazing, nice written and include approximately all significant infos. I?d like to see more posts like this.
One thing is the fact that one of the most prevalent incentives for making use of your card is a cash-back as well as rebate offer. Generally, you’re going to get 1-5 back on various expenditures. Depending on the card, you may get 1 returning on most expenditures, and 5 in return on purchases made at convenience stores, filling stations, grocery stores along with ‘member merchants’.
I’m not that much of a internet reader to be honest but your blogs really nice, keep it up! I’ll go ahead and bookmark your site to come back in the future. All the best
Magnificent beat ! I wish to apprentice while you amend your website, how can i subscribe for a blog website? The account helped me a acceptable deal. I had been tiny bit acquainted of this your broadcast provided bright clear idea
Excellent read, I just passed this onto a friend who was doing some research on that. And he just bought me lunch since I found it for him smile So let me rephrase that: Thank you for lunch!
Today, I went to the beach with my children. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is totally off topic but I had to tell someone!
Hello! I simply would like to give an enormous thumbs up for the great information you could have here on this post. I will be coming back to your weblog for more soon.
Do you have a spam problem on this website; I also am a blogger, and I was wanting to know your situation; many of us have created some nice practices and we are looking to exchange methods with other folks, please shoot me an email if interested.
I have realized that in digital cameras, unique devices help to {focus|concentrate|maintain focus|target|a**** automatically. The sensors involving some surveillance cameras change in contrast, while others employ a beam associated with infra-red (IR) light, particularly in low lumination. Higher spec cameras sometimes use a blend of both devices and might have Face Priority AF where the camera can ‘See’ any face and focus only in that. Thanks for sharing your ideas on this blog.
I have not checked in here for a while since I thought it was getting boring, but the last several posts are good quality so I guess I?ll add you back to my daily bloglist. You deserve it my friend 🙂