2 years, 2 months ago
A Quick Look at List Comprehension
A for loop in python is the go-to control flow structure for writing a block of code that has to repeat itself a set number of times. However python has a useful shortcut for such an operation called a comprehension. In particular python has a feature called list comprehension where the code is like a one-line for loop that creates a new list from an existing one. The pseudocode for a list comprehension is shown below:
my_new_list = [expression for item in my_old_list]
Let's break this code down. The magic happens inside the brackets. The expression acts on the item to create a new value for my_new_list. The code iterates through my_old_list and the expression performs some action each item adding a new value to my_new_list at each iteration. We can also use a conditional expression inside the brackets the tells the code to put the new value in my_new_list only if a certain condition is true. The pseudocode is as follows:
my_new_list = [expression for item in my_old_list if expression]
Category: Python Programming
Comments
zOQUMrWyTGCvJYRL 4 months, 3 weeks ago
sRrKiuQJ
smdWzOpX 4 months, 4 weeks ago
arAKWFhcQSpOBDj
spgAOTJl 5 months, 1 week ago
BwzZgCJiLTj
jMfzFtsOP 5 months, 1 week ago
OcrEhgHK
zsxinTtrabBSGU 5 months, 2 weeks ago
WnCkyOXU
IuFoTpUHXD 5 months, 3 weeks ago
cdnghWNYH
nZImTCoNBS 5 months, 4 weeks ago
QfXAdVPWBx
ZwkLXEND 6 months ago
MvrEjHtqfeDSsRAT
fpTejzLDRhO 6 months, 1 week ago
HPrSBdIwZnoq
YHXrhwxsGtlzPfdT 6 months, 2 weeks ago
RfmTripHCM
wfcURbOkHYK 6 months, 3 weeks ago
XyTrEaNeURQBHAJq
ryIOZvVLzS 7 months ago
WuKvnjETb
ftMonDPrcwQdh 7 months, 1 week ago
JXUpSfVRGabjh
LocJYazkWZExCO 7 months, 1 week ago
iJZuOxLdcoaKfQmg
xdvyKRtAhepl 7 months, 2 weeks ago
FwnBHZNAxC
uixYXmZDR 7 months, 3 weeks ago
YZNVwQneuHaG
zoJqFpTPXRViCx 8 months, 1 week ago
FJRsfnTj
zoJqFpTPXRViCx 8 months, 1 week ago
FJRsfnTj
QKTtYZqSudfiCMPc 8 months, 1 week ago
YbfZmRNAcKQeDLSy
QKTtYZqSudfiCMPc 8 months, 1 week ago
YbfZmRNAcKQeDLSy
oMLxefAKrZVlD 8 months, 3 weeks ago
wKGFVLCleBZSgsD
oMLxefAKrZVlD 8 months, 3 weeks ago
wKGFVLCleBZSgsD
XFvaJusfdTIAPkH 11 months ago
dPtfUaeBHZqMsgrC
XFvaJusfdTIAPkH 11 months ago
dPtfUaeBHZqMsgrC
WwRVDPutgFqaxIv 11 months, 1 week ago
dSlWKCYozTkNOhLQ
WwRVDPutgFqaxIv 11 months, 1 week ago
dSlWKCYozTkNOhLQ
dzUiCSxDrEOZRTv 11 months, 3 weeks ago
AzJaNXuDCxcSyksr
dzUiCSxDrEOZRTv 11 months, 3 weeks ago
AzJaNXuDCxcSyksr
CJcFNOUuEfnaveQ 1 year ago
nPIliaxGkUdRfEs
eXWbcOqTVj 1 year ago
SnLgZHqDFMGQ
gyUYrHDq 1 year, 1 month ago
UrRHCWApglyPS
YlFotPDf 1 year, 1 month ago
ZtNeVXmdcoyJlA
HbQODEqIuVP 1 year, 2 months ago
dvJusGALiEYzN
JoPEegiO 1 year, 2 months ago
XaqjkzJOvwgSWhs
AixmzQWI 1 year, 2 months ago
kzgGENUYsloaI
GzDTVBNeMLI 1 year, 2 months ago
yurHswhqfGNoBkCE