Python List of Strings With For Loop

When searching Python List of Strings you probably only find examples that print text. We go beyond that applying this to Cozmo and Vector robots in Python.

If you’re looking for another look at Python Lists of Strings that doesn’t only Print the results, you’re in the right place.

In the end we will be using For Loops to Iterate through the Strings within the List. We will be showing you code before that though which don’t follow best practices. This is to show you why Python For Loops are awesome for this sort of thing.

Python Lists

A Python List is a Container that can store multiple objects. For example, you might want to keep track of the scores for students on a test.

scores = [95, 87, 94, 82, 88]

While this Python List is storing integers, you can also store other data types including Strings.

Python Strings

A Python String is a series of characters. To assign a String typically you will use quotes around it.

thisString = “This is a string right here.”

Python String List

A Python List of Strings is exactly what it sounds like, a list that contains strings.

We can make a program that uses a list of strings such as:

thingsToSay = [“Hello.”, “Its Keith.”, “Today we’re talking about Python List of Strings.”]

This list contains 3 strings which we can then use.

Generally if you were learning about Python List of Strings, they’ll show you things like print(thingsToSay[0]) etc.

At Kinvert we like to keep things a bit more interesting than that and offer different ways to learn.

Time to take a look at how we can do something similar Python Lists of Strings using robots. In our case, Cozmo and Vector using the Anki Cozmo SDK and the Anki Vector SDK.

Cozmo Using Python List of Strings

When working with Lists, much like with Variables, we must define it before we can use it.

To create a List in Python you give it a name, use the equals operator to assign it a value, then put the individual values between square braces, with each value separated by a comma.

cozmoSentences = [“Here is the first sentence.”, “Another sentence for this Python List of Strings.”, “OK last one my fingers are tired.”]

We can have Cozmo and Vector to a bunch of different things with Strings in Python. For now, let’s have them talk.

Here is the code for Cozmo using a Python List of Strings.


"""
Copyright Kinvert All Rights Reserved
If you would like to use this code for
business or education please contact
us for permission at:
www.kinvert.com/
"""
import cozmo

def say_list_index(robot: cozmo.robot.Robot):
    #This is our Python String List
    list1 = ['Hello', 'Its Keith', 'Today we are talking about Python lists of strings']
    robot.say_text(list1[0]).wait_for_completed()
    robot.say_text(list1[1]).wait_for_completed()
    robot.say_text(list1[2]).wait_for_completed()
    
cozmo.run_program(say_list_index)

Keep in mind this code does not use best practices. If you wanted to add more things for Cozmo to say, you’d have to add more robot.say_text() commands.

In a bit, we’ll look at a better way to do this using Python For Loops.

Vector Using Python List of Strings

This first example for Vector will not be using best practices, similarly to the Cozmo example above.

We are going through the Python List of Strings using the index.

Once again, we’re facing the issue of what happens if we want to add more Strings to this Python List.


"""
Copyright Kinvert All Rights Reserved
If you would like to use this code for
business or education please contact
us for permission at:
www.kinvert.com/
"""
import anki_vector

def main():
    args = anki_vector.util.parse_command_args()
    #This is our Python String List
    thingsToSay = ['Hello.', 'Its Keith.', 'Today we are talking about Python Lists of Strings']
    with anki_vector.Robot(args.serial) as robot:
        robot.say_text(thingsToSay[0])
        robot.say_text(thingsToSay[1])
        robot.say_text(thingsToSay[2])

if __name__ == "__main__":
    main()

Soon we will be looking at a better way to do this with Python For Loops.

Cozmo – Python For Loop String List

Time to take a look at how we can handle Python Lists of Strings using best practices.

We want a program where if we want to increase the number of things Cozmo says, we only need to change the list of strings itself. We shouldn’t have to change anything else.

The way to accomplish this is with a Python For Loop which will iterate through the List and get us each individual String.


"""
Copyright Kinvert All Rights Reserved
If you would like to use this code for
business or education please contact
us for permission at:
www.kinvert.com/
"""
import cozmo

def say_list_with_for_loop(robot: cozmo.robot.Robot):
    #This is our Python String List
    thingsToSay = ['Hello', 'Its Keith', 'Today we are talking about Python lists of strings']
    for thing in thingsToSay:
        robot.say_text(thing).wait_for_completed()
    
cozmo.run_program(say_list_with_for_loop)

We can now add to the list, or take away from it, and the code will still work. We don’t need to add or remove any robot.say_text() calls.

Vector – Python For Loop String List

As with the Cozmo example above, we want it so if we add or remove items from the list, we don’t need to change anything else.

We are accomplishing this by getting rid of direct index calls and instead doing this inside an iterating For Loop.


"""
Copyright Kinvert All Rights Reserved
If you would like to use this code for
business or education please contact
us for permission at:
www.kinvert.com/
"""
import anki_vector

def main():
    args = anki_vector.util.parse_command_args()
    #This is our Python String List
    thingsToSay = ['Hello.', 'Its Keith.', 'Today we are talking about Python Lists of Strings']
    with anki_vector.Robot(args.serial) as robot:
        for thing in thingsToSay:
            robot.say_text(thing)

if __name__ == "__main__":
    main()

We are now having the robot say each item in the list. We can add or remove extra sentences from the list and the code will still work.

Going Further

We’ll write more about this sort of stuff in the future.

For now, here are some of the other things you might be interested in.

For more Cozmo stuff check out Cozmo Examples, Cozmo Games, and Cozmo Vs Vector.

More Vector stuff can be found at Vector Examples and Vector Games.

If Python seems a bit tough for now you might consider reading What is Robotics, Block Coding, and Age to Teach Kids Python.

What is ABS, What Does ABS Stand For, Flexible TPU Filament, and 3D Printer Repair are some other things we’ve written about in the past.

We also have an amazing mailing list which can keep you up to date with all things Cozmo and Vector. You can unsubscribe at any time.

Leave a Reply

Your email address will not be published. Required fields are marked *