Lists in Python
kster
Posted on October 18, 2022
Python has a built-in list type named 'list'. Lists are ordered, changeable, and allow duplicate values.
Lets try creating a list
heights = [162, 162, 152, 182, 213]
To create a list we have to create a variable. In this case
heights
is our variable that will store a list of integers into a list.Lists begin and end with square brackets.
Each of the items in the lists are separated with a comma.
*It's considered good practice to insert a space after each comma
Now, what if we wanted to add another heights to the list? We can use append() method, this will add an item to the end of the list.
Syntax to add an item to a list using .append
listname.append(x)
To remove an item from the list you can use .remove() this removes the first item from the list whose value is equal to x. An ValueError will be raised if there is no such item.
listname.remove(x)
Posted on October 18, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.