Select to view content in your preferred language

Python Help For Beginner

1146
6
Jump to solution
06-03-2014 08:16 AM
BrettAuger1
Deactivated User
Hi folks,

Another python question for the forum, this one should be easy. I am trying to complete the following exercise and am stumped with the last section. I am asked to print only the items in the list with exactly 5 characters. If anyone can help me out that would be much appreciated.

# 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 


Thanks,

Brett
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
Python has a length() test:

# 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


View solution in original post

0 Kudos
6 Replies
by Anonymous User
Not applicable
Python has a length() test:

# 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


0 Kudos
BrettAuger1
Deactivated User
Perfect, works just fine.

Thank you.
0 Kudos
JamesCrandall
MVP Alum
Or...


List = [value1, value2, value3, value4]
matches = [s for s in List if len(s)==5]
for m in matches:
  print m

0 Kudos
JamesCrandall
MVP Alum
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

0 Kudos
by Anonymous User
Not applicable
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.
0 Kudos
JamesCrandall
MVP Alum
Thanks James.  This is definitely the better answer, but list comprehensions are probably not very easy to understand for a beginner.


Heck, I still struggle with them!

OP -- Caleb's answer is exactly how I have it in 99% of my code but I'm starting to write in a more pythonic style.  I just thought a different approach would be good to see!
0 Kudos