JNTUK R16 UNIX AND SHELL PROGRAMMING UNIT 1 - 3
netaji gandi
Wednesday, July 31, 2019
list_variable = [x for x in iterable]
for clause. The in keyword is used as it is in for loops, to iterate over the iterable.netaji_letters = [letter for letter in 'netaji']
print(netaji_letters)
_letters, and letter is used to stand in for the items
contained in the iterable string 'netaji'.netaji_letters looks like, we call for it to print() and receive the following output:Output
['n', 'e', 't', 'a', 'j' , 'i']
'shark', that is, one string for each letter.for loops, though not every for loop is able to be rewritten as a list
comprehension.shark_letters list above, let’s rewrite it as a forloop. This may help us better understand how the list
comprehension works.netaji_letters = []
for letter in 'netaji':
netaji_letters.append(letter)
print(netaji_letters)
for loop, the variable assigned to the list needs to be
initialized with an empty list, as it is in the first line of our code block.
The for loop then iterates over the item,
using the variable letter in
the iterable string 'netaji'.
Within the for loop,
each item within the string is added to the list with
the list.append(x) method.for loop provides us with the same output:Output
['n', 'e', 't', 'a', 'j', 'i']
for loops, and some for loops can be rewritten to be list comprehensions to
make code more succinct.if statement used in a list comprehension:fish_tuple = ('blowfish', 'clownfish', 'catfish', 'octopus')
fish_list = [fish for fish in fish_tuple if fish != 'octopus']
print(fish_list)
fish_tuple as the basis for the new list
called fish_list. The keywords of for and in are used, as they were in the section above,
and now an ifstatement
is added. The if statement
says to only add those items that are not equivalent to the string 'octopus', so the new list only takes in items
from the tuple that do not match 'octopus'.fish_list contains the same string items as fish_tuple except for the fact that the
string 'octopus' has been omitted:Output
['blowfish', 'clownfish', 'catfish']
range()sequence type.number_list = [x ** 2 for x in range(10) if x % 2 == 0]
print(number_list)
number_list, will be populated with the squared
values of each item in the range from 0-9 if the
item’s value is divisible by 2. The output is as follows:Output
[0, 4, 16, 36, 64]
x for x in range(10). Our small program and output would
then look like this:number_list = [x for x in range(10)]
print(number_list)
Output
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
number_list = [x for x in range(10) if x % 2 == 0]
print(number_list)
Output
[0, 2, 4, 6, 8]
if statement has limited the items in the final list
to only include those items that are divisible by 2, omitting all of the odd
numbers.x squared:number_list = [x ** 2 for x in range(10) if x % 2 == 0]
print(number_list)
[0, 2, 4, 6, 8] are now squared:Output
[0, 4, 16, 36, 64]
if statements with a list comprehension:number_list = [x for x in range(100) if x % 3 == 0 if x % 5 == 0]
print(number_list)
x is divisible by 3, and then check to see if x is divisible by 5. If x satisfies both requirements it will print, and the
output is:Output
[0, 15, 30, 45, 60, 75, 90]
if statements can be used to control which items from
an existing sequence are included in the creation of a new list.for loop construction and work our way towards a list
comprehension.for loop code block:my_list = []
for x in [20, 40, 60]:
for y in [2, 4, 6]:
my_list.append(x * y)
print(my_list)
Output
[40, 80, 120, 80, 160, 240, 120, 240, 360]
x * y operation. This will be followed by the outer for loop, then the inner for loop. We’ll add a print() statement below our list
comprehension to confirm that the new list matches the list we created with our
nested for loop block above:my_list = [x * y for x in [20, 40, 60] for y in [2, 4, 6]]
print(my_list)
Output
[40, 80, 120, 80, 160, 240, 120, 240, 360]
for loops and flattens them into one line of code while
still creating the exact same list to assign to the my_list variable.Smart India Hackathon (SIH) 2025: Complete Guide with Problem Statements, Timeline & Idea PPT Template Lets Code • September 8, 2025 I...