Interesting ways to use List Comprehension
List comprehension is a powerful tool that allows Python programmers to write codes in an extremely condensed fashion. In this article we will go over how to use list comprehension to simplify otherwise complex functions.
Let’s begin!
To start off, let’s create a sample string.
sample_string = 'sample_for_list_comprehension'
Now let us create a Python list that contains every character of that string as its elements.
sample_list =[]
for s in sample_string:
sample_list.append(s)
sample_list
Creating a List
We can actually generalize this code and turn it into a Python function. The below code does just that.
def list_creator(input_string):
final_list = []
for i in input_string:
final_list.append(i)
return final_list
sample_list = list_creator(sample_string)
sample_list
🔵 Intellipaat Data Science with Python course: https://intellipaat.com/python-for-data-science-training/In this Data Science With Python Training video, you...
Enroll in our Data Science with Python training in Chennai. Best Data Science with Python Training courses in Chennai for 100% Job Placements Support.
🔥Intellipaat Python for Data Science Course: https://intellipaat.com/python-for-data-science-training/In this python for data science video you will learn e...
Master Applied Data Science with Python and get noticed by the top Hiring Companies with IgmGuru's Data Science with Python Certification Program. Enroll Now
Online Data Science Training in Noida at CETPA, best institute in India for Data Science Online Course and Certification. Call now at 9911417779 to avail 50% discount.