kster

kster

Posted on October 18, 2022

Lists in Python

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]
Enter fullscreen mode Exit fullscreen mode
  1. 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.

  2. Lists begin and end with square brackets.

  3. 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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
kster
kster

Posted on October 18, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Lists in Python
python Lists in Python

October 18, 2022

Python Polymorphism
python Python Polymorphism

October 2, 2022