Python Lists

A list is a collection of objects. It contains the following features:

  • It is ordered
  • It is changeable
  • It allows duplicate members

Lists are start at position 0. You can use slice notation like we covered in the strings chapter

You can use a for loop to iterate through a list.

Use the “in” keyword to check if an item exists in a list.

Use the len() method to get the length of a list

Use the sort() method to sort a list.

Use the reverse() method to reverse sort a list.

Add a single item to a Python list using the append() method.

Add multiple items to a Python List using the extends() method.

To add an item to a list at a specific location use the insert() method.

To remove an item from a Python List by index use the pop() method.

To remove an item from a Python List by index use the del keyword.

To remove an item from a Python List by value use the remove() method.

To remove all items from a Python List use the clear() method.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store