# Create list with these four entries: roads, streams, parks, contours value1 = "roads" value2 = "streams" value3 = "parks" value4 = "contours" List = [value1, value2, value3, value4] # Print the first entry in the list to the interactive window print List[0] # Use a loop to print every entry that has exactly 5 # characters to the interactive window print List
Solved! Go to Solution.
# Create list with these four entries: roads, streams, parks, contours value1 = "roads" value2 = "streams" value3 = "parks" value4 = "contours" List = [value1, value2, value3, value4] # Print the first entry in the list to the interactive window print List[0] # Use a loop to print every entry that has exactly 5 for val in List: if len(val) == 5: print '{0} has a length of 5 characters'.format(val) # characters to the interactive window
# Create list with these four entries: roads, streams, parks, contours value1 = "roads" value2 = "streams" value3 = "parks" value4 = "contours" List = [value1, value2, value3, value4] # Print the first entry in the list to the interactive window print List[0] # Use a loop to print every entry that has exactly 5 for val in List: if len(val) == 5: print '{0} has a length of 5 characters'.format(val) # characters to the interactive window
List = [value1, value2, value3, value4] matches = [s for s in List if len(s)==5] for m in matches: print m
value1 = "roads" value2 = "streams" value3 = "parks" value4 = "contours" List = [value1, value2, value3, value4] for match in [s for s in List if len(s)==5]: print match
Even more pythonic:value1 = "roads" value2 = "streams" value3 = "parks" value4 = "contours" List = [value1, value2, value3, value4] for match in [s for s in List if len(s)==5]: print match
Thanks James. This is definitely the better answer, but list comprehensions are probably not very easy to understand for a beginner.