Python List of Strings Cozmo / Vector

Using a Python List of Strings we will get Cozmo to talk. We can store multiple words in the Python List of Strings and Cozmo speaks each String.

In Python you can make lists of basically anything. You can even make lists of lists.

In this case we’re going to use the Anki Cozmo SDK, Anki Vector SDK, and Python to create a list of strings.

With this Python list of strings, we can do multiple things. We will use multiple methods to make Cozmo talk based on the list we make.

This is one of many Anki Cozmo Python Examples. We also have a lot of Anki Vector Examples.

Generally when you find Python Tutorials online they’ll just Print things out. At Kinvert we like to shake things up a bit. Here we’ll be controlling robots instead.

Python Lists

What is a Python list? It’s a type of container that can store more than one object. Imagine if you have a class full of kids, you could make a list of their scores.

That said, you’d need to know what order to put them, for example, alphabetically.

This isn’t the best way to do it. In real life you’d do this in an Object Oriented way. But it’s a quick and easy example to show.

Your list would look something like:

grades = [95, 82, 75, 96, 90]

Not many kids in that class, but at least their average score was good, right?

Python Strings

In Python, Strings are a series of characters surrounded by quotes. For example:

string = “This is a string right here”

You can do many things with Strings. Generally when working with Cozmo, if we’re working with strings it’s likely to have him speak.

Lists can store many things in Python, and that includes Strings.

We already have an example of Cozmo Speaking With Strings and robot.say_text().

Also there is a slightly more advanced example of Vector Using Strings and say_text().

Python List of Strings

Now we can start looking at what a Python list of strings looks like. For example:

cozmoSays = [“Hello.”, “It’s Keith from Kinvert.”, “Today we will be talking about Python Lists of Strings”]

Keep in mind you can’t just have Cozmo directly say the List. We’ll go over that in a bit.

Cozmo’s say_text() method takes in a string as a parameter, not a list of strings.

Using a Python List of Strings by Index

Lists in Python have an Index that begins at Zero.

So the ‘first’ item in the list is the zeroth item in the list. The first thing we say in the list is “Hello” which means well be accessing the index of zero for that list. List1[0] will get us “Hello”.

Here is the code for Cozmo:


"""
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/
Free for personal use
"""
import cozmo

def kinvert_cube_lights(robot: cozmo.robot.Robot):
    for pythong
    robot.say_text(list1[2]).wait_for_completed()
    
cozmo.run_program(kinvert_cube_lights)

This example didn’t use best practices. What if later on someone changes how many items are in the list? You would either have to add new lines, or worse yet, you’d forget and not say everything.

In Python we can iterate through Lists with For Loops.

Now let’s take a look at an Anki Vector Example.


"""
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/
Free for personal use
"""

import anki_vector

def main():

    args = anki_vector.util.parse_command_args()

    listOfStrings = ['Hello.', 'Its Keith', 'We are learning about Python lists of strings']
    with anki_vector.Robot(args.serial) as robot:
        robot.say_text(listOfStrings[0])
        robot.say_text(listOfStrings[1])
        robot.say_text(listOfStrings[2])

if __name__ == "__main__":
    main()

Again this has the same issue as before with the Cozmo example where we hard coded in some values.

Now, let’s take a look at how we can get around this bad practice.

Iterating Through Python List With Loop

A better way to iterate through a Python list is with a for loop.

Starting with our previous Python list of strings code, we’ll make some modifications to make some improvements.


"""
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/
Free for personal use
"""
import cozmo

def kinvert_cube_lights(robot: cozmo.robot.Robot):
    list1 = ['Hello', 'Its Keith', 'Today we are talking about Python lists of strings']
    for pythonString in list1:
        robot.say_text(pythonString).wait_for_completed()
    
cozmo.run_program(kinvert_cube_lights)

Let’s apply the same changes to the Vector code.


"""
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/
Free for personal use
"""

import anki_vector

def main():

    args = anki_vector.util.parse_command_args()

    listOfStrings = ['Hello.', 'Its Keith', 'We are learning about Python lists of strings']
    with anki_vector.Robot(args.serial) as robot:
        for pythonString in listOfStrings:
            robot.say_text(pythonString)

if __name__ == "__main__":
    main()

This is an improvement for multiple reasons.

We have fewer lines of code, it is more readable for people with experience in Python, and if we add or remove items in the list we won’t have to change anything else to keep the code working as intended.

If In

You can check to see if a certain string is inside a list of strings.

Take the code from the previous section, and say something like:

if ‘Hello’ in lostOfStrings:

robot.say_text(“Yes that is in the list of strings”)

List Length

You can find the number of items in a List. This is called the Length. To find the number of strings in the list of strings, use len(LIST).

So for example, you can do robot.say_text(len(listOfStrings))

Append

In Python, to Append to a List, you use listName.append(thingToAppend)

So we could do listOfStrings.append(‘The End’)

Remove

Based on what you know about how to Append to a list, how do you think you’d remove an item from a list?

Think about it before reading the next line.

listOfStrings.remove(‘The End’) is how you’d remove the string you added to the list of strings in the last section.

Pop

Try adding in listOfStrings.pop(1) and see what happens.

Change Value of Item in List

listThing[2] = “New Text Here”

Copying a List

Can’t do list1 = list2 as they will reference each other.

list2 = list1.copy()

Count and Index

More to come on this in a bit.

In Conclusion

There is much more you can do with lists of strings in Python than what we had time for in this article.

They are very useful, and there are multiple ways to use them.

Lists have a certain number of items in them, and you can get this by finding the Length of the list.

There is always an Index for a list. Also, remember that a lists index begins at zero.

A List of Other Stuff You Might Like

If you made it this far, chances are you’re interested in STEM (Science, Technology, Engineering, and Math). Kinvert writes about a lot of STEM related topics.

For more Cozmo stuff check out Cozmo Vs Vector, Code Lab, Cozmo Education Mode, Homeschool Robotics, and Cozmo K-12 Curriculum.

We also have a bunch of stuff about Vector, such as Anki Vector SDK in Python, Anki Vector Python Examples, and Storytime Reading Robots.

3D Printing is another topic near and dear to our hearts. If you’re in to 3D Printing as well, check out What is ABS, What Does ABS Stand For, Flexible TPU Filament, and 3D Printer Repair.

If Python still seems a bit difficult, we have Block Coding, What is Robotics, Robot Dinosaurs, and Age to Teach Kids Python.

Finally, we have a mailing list that will keep you up to date with all things Cozmo. You can unsubscribe at any time.

Leave a Reply

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