List in python

list in python

A list in python is an ordered set in which each of its values is represented by an index. These values are termed elements. A list is like a string, which is an ordered set of characters.

How to Create List?

In lists, the elements can be of any type. These ordered sets like List or string are called sequences.

Examples:

There are various ways to create a list. 

([ these brackets are used ] ) (“[ ]”) 

  • [ 1, 2, 3, 4, 5 ] – It’s a list of five integers
  •  [ “book”, “pen”, “eraser”, “chalk” ] – it’s a list of four stationary items.

Nested List in python:

One list can hold another list i.e Nested List 

Example :

["batman", 1, [2, 9], 3.7] 

Consecutive Integers:

Lists handles the consecutive integers in an ease in python.

 Example:

[1, 2, 3, 4, 5] → range(1, 5) 

This range function consists of two arguments and delivers a list holding integers from first argument to second argument.

 Note: The first argument is included in the list whereas the second argument is excluded. Others types of Range Function. The range has two other forms as well.

Three Arguments Range

The third parameter gives the gap between two successive values (also called Step Size) 

Let’s understand by an example: range(2, 12, 2) 

This gives us the list as  : [2, 4, 6, 8, 10] 

• Single Argument : It creates a list that starts from 0. 

Example:

 [0, 1, 2, 3, 4]

 Empty List in python:

It does not contain any value. It is represented by []. 

So now we have created our list and we will pass them as an argument to the functions. 

Let’s understand it by few examples: 

room = [“fan”, “sofa”, “bed”] 
Sum = [13, 14] 
Print Sum, room 

How to Access the Elements

The syntax is same as the syntax for accessing the characters of a string using the bracket operator ( [] ). Index is represented inside the brackets.

 Note: Indices begin from 0.

Example:

print sum[2] 
                    
 add[1] = 4 

The bracket operator can be used anywhere in the expression. 

We use loops to access the index

 room = ["sofa", "bed", "pillow", "canvas"]
i = 0        // It will iterate for 4 times i.e. 0, 1, 2 and 3 and then it fails 
while i < 4: 
print room[i]
 i = i + 1 

Operations in List

The + operator: It concatenates the list.

x = [2,4,6] 
y = [1,3,5] 
z = x + y 
print z   

Output

=> [2, 4, 6, 1, 3, 5]

 * operator: It gives the number of time the value needs to be repeated.

Example:

[1] * 4 
# output
→ [1, 1, 1, 1]

[2, 4, 6] * 2 
# output
 → [2, 4, 6, 2, 4, 6] 

List Slices in python

 It works well with lists. Let us understand with

example.

num = ['1', '2', '3', '4', '5', '6'] 
    → list[1 : 4] 
['2', '3', ‘4’, ‘5’] 

→ list[ :4 ]
 ['0', '1', '2', '3']

→ list[ 3: ]
 ['4', '5', '6'] 

→  list[ : ]
 ['1', '2', '3', '4', '5', '6'] 

Mutable Lists in python

 Lists are mutable in nature, which means that we can change their elements.

country = ["India", "France", "Italy"] 
>>> country[0] = "USA"
>>> country[1] = "Sudan"
>>> print county  → ['USA', 'France', 'Sudan'] 

We can update several elements at a time.

Example:

list = ['1', '2', '3', '4', '5', '6'] 
list[1 : 3] = ['a', 'b'] 
print list 
['1', 'a', 'b', '4', '5', '6']

We can likewise expel components from a list by allotting the vacant list to them.

Example:

list = ['1', '2', '3', '4', '5', '6'] 
list[1 : 3] = [] 
print list ['1', '4', '5', '6']

List Deletion in python

It is an alternative to slice which can be error-prone. del function removes an element from a list. 

Example:

a = ['a', 'b', 'c'] 
del a[1] 
print a 
→['a', 'c'] 

del can handle negative indices and may lead to runtime error if the index is out of range.