You may already be familiar with arrays. In Python, they are known as lists. Learn how to create, append, remove, sort, and reverse lists in this lesson.
If you've used other programing languages, you may know these as arrays, but in Python, they are called lists. It's just a sequence of objects. It doesn't have to be the same data type, though. We can have a list that has an integer, a string, another integer. It can even contain a dictionary.
Individual elements can be accessed by their index numbers. To grab the first item from the list b, we can use square brackets and index number. To grab the second number, we would use the index number one.
Let's create an empty list called pets. We'll do that by specifying empty braces. Then we can add to the pets list with the append method. We can add a cat to it, we'll add a dog, we can add a bear to it, and we can add a shark. When we display the list, all of our items are added to it.
If we want to get rid of the dog, we can use the remove method, and specify the item that we want to remove. We can use the pop method to remove the last item from the list. If we want to specify which item we'd like to get rid of, we can call the pop method, and pass it an index number.
We add our pets back in. We can sort the list, which is going to sort the items alphabetically. You can reverse the list as well.
To find out how many items are in the list, you can use the LEN method, which is short for length. You can get the number of occurrences of a specific item in the list using the count method, passing in the list item that you would like to count, and it returns the number of instances found.
Member comments are a way for members to communicate, interact, and ask questions about a lesson.
The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io
Be on-Topic
Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.
Avoid meta-discussion
Code Problems?
Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context
Details and Context
Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!