Directory Image
This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Privacy Policy.

What is Python List?

Author: Mansoor Ahmed
by Mansoor Ahmed
Posted: Oct 31, 2020
  • In Python, a list is a mutable, means the values that it contain can be modified.

  • A list is a variable that can have a sequence of values assigned to it.

  • In a list, these values are known as elements or members.

  • It is collection which is ordered, changeable and allows duplicate members.

Defining a List

  • We define a list this way:

    fruits = ["Mango", "Apple", "Orange", "Banana", "Grapes"]

  • In Python lists are written with square brackets.

  • We define a list by enclosing everything to the right of the equal sign in square brackets.

  • Each element or member is separated by a comma and a space.

  • The first element in a list always has an index of 0, the second element an index of 1, and so on.

  • A list element can be assigned any type of value that we can assign to ordinary variables, for example a string or a number.

  • We can even mix the different types of values in the same list.

  • For example: collection = ["5","Abdulmomin","Where is"]

  • In this example, collection [0] has a numerical value of 5, collection [1] has a value of "Abdulmomin", and collection [2] has a value of "Where is".

    All the time remember that:

  • The first element in a list always has an index of 0, not 1. This means that if the last element in the list has an index of 9, there are 10 items in the list.

  • The same naming rules we learned for ordinary variables apply.

  • Only letters, numbers, and underscores are legal. The first character can't be a number. No spaces.

  • It's a good idea to make list names plural—fruits instead of fruit, for example—since a list usually contains multiple things.

Add items

  • We use the append () method to add an item to the end of the list.

  • If we want to add "Straw berry", in the end of the list fruits it will be as fruits = ["Mango", "Apple", "Orange", "Banana", "Grapes", "Straw berry"]

  • The statement begins with the list name:fruits.append("Straw berry")

  • Next there's a dot: fruits.append("Straw berry")

  • Then the keyword append: fruits. append("Straw berry")

  • Instead of appending an element to the end of a list, we can insert it into the list where we want it.

  • We use the insert () method to add an item at the specified index.

Delete and remove elements

There are several methods to remove items from a list.

  • We can delete any list element by specifying its index number.

  • The remove () method removes the specified item.

  • The pop () method removes the specified index. We can pop an element off a list and append it or insert it to another list.

  • The del keyword removes the specified index. It can also delete the list completely.

  • Similarly, the clear () method empties the list.

Slicing

  • We can copy consecutive elements of a list to build another list.

  • The first number inside the brackets targets the first element in the slice.

List Length

  • Use the len () function to determine how many items in a list.

    Example: Print the number of items in the list:

    fruits = ["Mango", "Apple", "Orange", "Banana", "Grapes", "Straw berry"]

    print(len(fruits))

    Result: 6

Concatenation of the list

  • In Python, two or more lists can be joined or concatenate.

  • One of the easiest ways are by using the + operator.

    Example: Join two list:

    List4 = ["x", "y", "z"]

    List5 = [5, 6, 7]

    List6 = list4 + list5

    print(list6)

  • Result: [ "x"."y"."z",5,6,7]

  • We can join two lists by appending all the items from list4 into list5, one by one.

  • We can also use the extend () method, which purpose is to add elements from one list to another list.

List Methods

We can use on lists the following set of Python built-in Method.

Method Description

append() Adds an element at the end of the list

insert() Adds an element at the specified position

extend() Add the elements of a list to the end of the current list

pop() Removes the element at the specified position

remove() Removes the item with the specified value

clear() Removes all the elements from the list

count() Returns the number of elements with the specified value

copy() Returns a copy of the list

index() Returns the index of the first element with the specified value

reverse() Reverses the order of the list

About the Author

Mansoor Ahmed Chemical Engineer,Web developer

Rate this Article
Leave a Comment
Author Thumbnail
I Agree:
Comment 
Pictures
Author: Mansoor Ahmed

Mansoor Ahmed

Member since: Oct 10, 2020
Published articles: 124

Related Articles