0% found this document useful (0 votes)
19 views1 page

Python List Comprehension 1677089695

The document provides examples of Python list comprehensions, demonstrating various operations such as multiplying, squaring, reversing, and modifying lists. It includes examples for checking values, determining lengths, and performing operations on nested lists. Each example shows the command and the resulting output, illustrating the flexibility and power of list comprehensions in Python.
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)
19 views1 page

Python List Comprehension 1677089695

The document provides examples of Python list comprehensions, demonstrating various operations such as multiplying, squaring, reversing, and modifying lists. It includes examples for checking values, determining lengths, and performing operations on nested lists. Each example shows the command and the resulting output, illustrating the flexibility and power of list comprehensions in Python.
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

Python List Comprehension

Prompt Command Result

variable = [***result*** for element in entity **entity could be range(), list,


Concept
if condition] or any iterable value…

lib = [4,8,2,4,0,3] >>> print(double_nums)


Multiply each number
double_nums = [num * 2 for num in lib] [8, 16, 4, 8, 0, 6]

lib = [4,8,2,4,0,3] >>> print(pow_nums)


POW each number
pow_nums = [pow (x, 2) for x in lib] [16, 64, 4, 16, 0, 9]

one = ['a', 'b', 'c', 'd', 'e', ‘f’] >>> print(two)


Reverse a list
two = [::-1] ['f', 'e', 'd', 'c', 'b', 'a']

names = [‘Bob’, ‘Mike’, ‘John’]


new_list = [“Hi, ” + name for name in names] >>> print(two)
String list change
or ['Hi, Bob', 'Hi, Mike', 'Hi, John']
new_list = [f ’Hi, {name}’ for name in names]

String call of the rst names = [‘Bob’, ‘Mike’, ‘John’, ‘Jerry’] >>> print(new_list)
letter new_list = [ x[0] for x in names] ['B', 'M', 'J', ‘J’]

Determine length of a names = [‘Bob’, ‘Mike’, ‘John’, ‘Jerry’] >>> print(lengths)


string lengths = [ len(x) for x in names] [3, 4, 4, 5]

booleans = [True, False, True] >>> print(result)


Opposite boolean
result = [not x for x in booleans] [False, True, False]

names = [‘Bob’, ‘Mike’, ‘John’, ‘Jerry’] >>> print(check)


Check for value I
check = [x == ‘John’ for x in names] [False, False, True, False]

lib = [4,8,2,4,] >>> print(check)


Check for value II
check = [x > 3 for x in lib] [True, True, False, True]

nl = [[4,8], [15,16], [23,42]] >>> print(check)


Nested list sum
sum=[x+y for x, y in nl] [12, 31, 65]

nl = [[4,8], [15,2], [23,42]] >>> print(check)


Nested list check
check = [x > y for x, y in nl] [False, True, False]

a = [5,1,6]
>>> print(new)
Sum two lists b = [3,2,4]
[8, 3, 10]
new=[x+y for x, y in zip(a, b)]

a = [5,1,6]
List Comprehension to >>> print(united)
b = [3,2,4]
concatenate lists [5, 1, 6, 3, 2, 4]
united = [ x for y in [a, b] for x in y]
fi

You might also like