0% found this document useful (0 votes)
62 views4 pages

Feb 01

The document discusses list comprehensions in Python. It shows how to create lists using for loops and using list comprehensions. Some key points covered include creating lists of even numbers from a range, creating lists from strings by converting characters to integers, and checking the types of elements in lists.

Uploaded by

Paranthaman G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views4 pages

Feb 01

The document discusses list comprehensions in Python. It shows how to create lists using for loops and using list comprehensions. Some key points covered include creating lists of even numbers from a range, creating lists from strings by converting characters to integers, and checking the types of elements in lists.

Uploaded by

Paranthaman G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Type "help", "copyright", "credits" or "license()" for more information.

>>> # list comprehension


>>> l = []
>>> # create a list l that contains only even int from 0 to 10
>>> [0, 2, 4, 6, 8, 10]
[0, 2, 4, 6, 8, 10]
>>> # Method #1 W/o comprehension
>>> for i in range(0, 11, 2):
l.append(i)

>>> l
[0, 2, 4, 6, 8, 10]

>>> # Method #2 W/ comprehension


>>> list_2 = [i for i in range(0, 11, 2)]
>>> list_2
[0, 2, 4, 6, 8, 10]

>>> # Method #1
>>> # iterate a for loop with range as req start = 0, stop =11, step =2
>>> # value ret by range is APPENDED to an empty list

>>> # Method # 2
>>> ## list_2a = [ <loop variable> || <for loop>]
>>> # <for loop> -> for i in range(0, 11, 2) ## Colons are omitted
>>> # <loop variable> -> i

>>> list_2a = [i for i in range(0, 11, 2)]


>>> list_2a
[0, 2, 4, 6, 8, 10]
>>> l = []
>>> for i in range(0, 11, 2):
l.append(i)

>>> # Goal of list comprehension to create a NEW LIST


>>> [ s for s in range(1, 10)]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> p = []
>>> for s in range(1, 10):
p.append(s)

>>> p
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> "123456789"
'123456789'
>>> # create a list of digits and each ele in list should be of type int
>>> q = []
>>> s = "123456789"
>>> s
'123456789'
>>> list(s)
['1', '2', '3', '4', '5', '6', '7', '8', '9']
>>> q
[]

>>> for i in s:
i_int = int(i) # convert each char in str to int
q.append(i_int) # append the converted integer to the list

>>> q
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> # Verify whether each element of list q is int
>>> for j in q:
print(type(j))

<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
>>> for j in q:
print(isinstance(j, int))

True
True
True
True
True
True
True
True
True
>>> q.append('21') # a value of type str is appended to demonstrate
usage of isinstance function
>>> q
[1, 2, 3, 4, 5, 6, 7, 8, 9, '21']
>>> for j in q:
print(isinstance(j, int))

True
True
True
True
True
True
True
True
True
False
>>> # because the last ele of q is of type str the isinstance returned False at the end
>>> q.pop() # revert q to it's original value
'21'
>>> q
[1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> q_comprehension = [int(i) for i in s]


>>> q_comprehension
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> q
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> q_comprehension_check_type = [isinstance(i,int) for i in
q_comprehension] # q_comprehension_check_type is list of the booleans such that
>>> # q_comprehension_check_type states whether q_comprehension[i] is an int type.
>>> # <i> is any index
>>> q_comprehension_check_type
[True, True, True, True, True, True, True, True, True]

>>> q_comprehension_check_type = [(isinstance(i,int), i) for i in


q_comprehension]
>>> # each ele of q_comprehension_check_type is a tuple with a pair of values
# the first element of tuple indicates whether the value is an int, the second ele the value itself
>>> q_comprehension_check_type
[(True, 1), (True, 2), (True, 3), (True, 4), (True, 5), (True, 6), (True, 7), (True, 8), (True, 9)]
>>> convert

>>> # w/o comprehension


>>> s
'123456789'
>>> w = []
>>> w_t_c = []
>>> counter = 0
>>> for i in s:
w.append(int(i))
w_t_c.append(isinstance(w[counter], int))
counter = counter + 1

>>> w
[1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> w_t_c
[True, True, True, True, True, True, True, True, True]

>>> w = []
>>> w_t_c = []
>>> counter = 0
>>> for i in s:
w.append(int(i))
w_t_c.append((isinstance(w[counter], int), w[counter]))
counter = counter + 1

>>> w
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> w_t_c
[(True, 1), (True, 2), (True, 3), (True, 4), (True, 5), (True, 6),
(True, 7), (True, 8), (True, 9)]

You might also like