DEV Community 👩‍💻👨‍💻

Cover image for Lists in Python

Posted on • Updated on

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

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.

Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Let's Get Wacky

Use any Linode offering to create something unique or silly in the DEV x Linode Hackathon 2022 and win the Wacky Wildcard category

Join the Hackathon <-