info@worthwebscraping.com
(+91) 79841 03276

List in python – Create list | Append list | Insert & Delete element in List | Slicing

List in python - Create list | Append list | Insert & Delete element in List | Slicing

Download Python Script

Send download link to:

The list type is a container that holds a number of other objects, in a given order. The list type implements the sequence protocol, and also allows you to add and remove objects from the sequence.

Creating Lists

To create a list, put the number of expressions in square brackets:

  L = []

  L = [object, …]

The objects can be anything; you can assign all kinds of objects in lists, including other lists, or also multiple references to a single object.

You can also use the built-in list type object to create lists:

  L = list() # empty list

  L = list(sequence)

You can also use the built-in list type object to create lists:

  L = list() # empty list

  L = list(sequence)

The sequence can be any kind of sequence object or iterable, including tuples. If you pass in another list, the list function makes a copy. A list can contain mixed data types like integer, floats, strings etc.

Finding the Length of the List

List has len() method which returns the length of a list.

Output:

6

Add an Element to the End of the List – Append

To add an element at the end of list append() method is used.

Output:

As we can see string any is added at the end.

Insert an Element at a Particular Position in the List

Insert() method is used to add the element at any particular required position/index. Insert will take two arguments, the index and the element that needs to be added.

Output:

List in a List – Append vs Extend

In the above example we saw, append() method adds any element at the end of the list. If we use append() method to add another list within a list it will add it as it is at the end.

Output:

The extend() method is used to insert the individual elements of the list instead of list itself, however this method is used to add one list to another only.

Output:

Delete an Element in a List – del vs pop vs remove

To remove elements from a list we have three methods del, pop and remove. The del statement can be used to remove an individual item, or to remove all items identified by a slice. The pop method removes an individual item and returns it, while remove searches for an item, and removes the first matching item from the list.

Output:

Output:

The del statement and the pop method does pretty much the same thing, except that pop returns the removed item.

Membership of List – in and not in

We can check if an object is in a list or not using in and not in. This is a very useful method which is used extensively while writing loops etc.

Output:

Reversing a List – reverse

reverse() method reverses a list completely, meaning first element becomes last and last becomes first.

Output:

Output:

Sorting Lists 

We can sort a list by ascending or descending order. There are two methods available for this.

The sort method sorts a list in place.

  L.sort()

To get a sorted copy, use the built-in sorted function:

  out = sorted(L)

An in-place sort is slightly more efficient, since Python does not have to allocate a new list to hold the result.

Accessing Items of a List

We can access any item of a list or number of items using indexing. Python list have indexes for all the items it contains. Index starts from 0 i.e the first element is at 0 index and not 1. Accessing elements in a list is called indexing. To access any element just pass the index of that element:

Output:

Output:

List Slicing

Accessing parts of segments is called slicing. Slicing is done using colon (:) symbol.

Start index : End Index. Value at start index is included but not the value at end index.

Output:

That’s all the basic on List. Go ahead and start working with python Lists.