Python Tutorial 10: Basic operation of Python List

What is Python List?

The most basic data structure in Python is the sequence. Each element of a sequence is assigned a number – its position or index. The first index is zero, the second index is one, and so forth.

Python has following six built-in types of sequences:

  • str,
  • unicode,
  • list,
  • tuple,
  • buffer,
  • xrange

The most common sequences in Python are list and tuple. This tutorial will go through some basic functions for a list.

All sequences can do following operations:

  • indexing
  • slicing
  • multiplying
  • checking for membership.
  • finding the length of a sequence
  • finding largest and smallest elements

Python Lists

The list is a most adaptable datatype available in Python. Following example illustrated how to create a list in Python.

European_Sales_List = [“Peter”, “David”, “Mary”,”Joan”]
American_Sales_List = [“John”,”Tony”,”Michelle”,”Mark”]

 

0123
European_Sales_ListPeterDavidMaryJoan
American_Sales_ListJohnTonyMichelleMark

 

This will create two lists each of them with 4 elments they are separated by a comma.

Important thing about a list is that items in a list need not be of the same type.

 

Accessing Values in Lists

To access values in lists, use the square brackets for slicing along with the index or indices to obtain value available at that index. For example −

print(European_Sales_List=[1])

Sample For accessing list item in Python

 

 

 

 

Updating Lists

You can update single or multiple elements of lists by giving the slice on the left-hand side of the assignment operator, and you can add to elements in a list with the append() method.

Update Single Elements

For Example to change the element 1 in the European Sales List from “David” to “Daniel”

0123
European_Sales_ListPeterDavid DanielMaryJoan
American_Sales_ListJohnTonyMichelleMark

European_Sales_List[1] =”Daniel”

Update one single element - Python List

Merge two list and store new result into a new list

All_Sales_List = European_Sales_List + American_Sales_List

Merge two python list into one - Python List

 

Append List Elements

There is two options we can add an element into a list:

  1. Use the “+” Operator
  2. User the append function

Using the “+” Operator

Following example added a a new element “Albert” into the list “American_Sales_List using the ” + ” operator

Add element to python list using the "+"operator

Using the append function

Following example added a a new element “Albert” into the list “American_Sales_List using the append function

Syntax:

append(element)

Add element to python list using the append function

“+” Operator vs Append Function

The + operation adds the list elements to the original array. You can see that from my previous example, the new element “Albert” was in a square bracket. This means the element “Albert” was being treated as a list before being added to the “American_Sales_List”

The array.append operation inserts the array (or any object) into the end of the original array, which results in a reference to self in that spot (hence the infinite recursion).

The difference here is that the + operation acts specific when you add an array (it’s overloaded like others, see this chapter on sequences) by concatenating the element. The append-method however does literally what you ask: append the object on the right-hand side that you give it (the array or any other object), instead of taking its elements.

 

Delete List Elements

To remove a list element, you can use either the del statement if you know exactly which element(s) you are deleting or the remove() method if you do not know. For example −

Del Statement

Syntax:

Del <List  Name> [<index>]

Delete an element from Python List

Remove Statement

Syntax:

List_name.remove(Element to remove)

Remove an element from Python List

 

Basic List Operations

Lists respond to the + and * operators much like strings; they mean concatenation and repetition here too, except that the result is a new list, not a string.

In fact, lists respond to all of the general sequence operations we used on strings in the prior chapter.

Python ExpressionResultsDescription
len(American_Sales_List)3Length
All_Sales_List = European_Sales_List + American_Sales_List[‘Peter’, ‘Daniel’, ‘Mary’, ‘Joan’, ‘John’, ‘Tony’, ‘Michelle’, ‘Mark’]Concatenation
[‘Hi!’] * 4[‘Hi!’, ‘Hi!’, ‘Hi!’, ‘Hi!’]Repetition
‘Mary’ in All_Sales_ListTrueMembership
for x in All_Sales_List : print (x, End =’, ‘)Peter, Daniel, Mary, Joan, John, Tony, Michelle, Mark,Iteration

 

 

Leave a Comment