Latest
Loading...

Python for Ethical Hacking

Command Prompt

Featured Post

#5 Jarvis

Jarvis      Now we are going to create some functions. With the help of this functions we are going to make our Play music Open website...

List datatype || Python for beginners

List datatype || Python for beginners

List 

  • A list is an ordered sequence of any type of values and can be created as a set of comma separated values within square brackets.
  • The values in a list are called elements.


Syntax:

Listname=[ ]  #empty list

Listname=[element1, element2, element3]  #List with elements 


Nested List

A list within another list is called nested list.

Example:

List=[‘mari’,[1,2,3]]


Accessing elements in List

Indexing and slicing can be used to access list elements. The indices of list elements are numbered from 0 from the left end and numbered from -1 from right end.

Example 

list=['mari',2017,9.5]

list[0]

List[2]

List[-1]

Output

'Mari'

9.5

9.5



List operation

  1. + represents concatenation
  2. * represents repetition


Example

List1=[1,2,3]

List2=[4,5]

List3=List1+List2

List4=List1*2

print(List3)

print(List4)

Output

[1,2,3,4,5]

[1,2,3,1,2,3]


List slices

  1. A part of a list called list slice. The operator [m:n] returns the part of the list from 𝑚𝑡ℎ index to 𝑛𝑡ℎ index including the element at 𝑚𝑡ℎ index but excluding the element at 𝑛𝑡ℎ index.
  2. If the first index is omitted the slice starts at the beginning of the list. If the second index is omitted, the slice ends at the end of the list. If both indices are omitted, the slice is the given list itself.


Example

List=[1,2,3,4,5]

List[0]

List[:4]

List[1:]

List[1:1]

List[5:1]

List[:]

Output

1

[1,2,3,4]

[2,3,4,5]

[]

[]

[1,2,3,4,5]


List methods

  • listname.append(element)
  • listname.count(element)
  • listname.extend()
  • listname.inset()
  • listname.index()
  • listname.pop()
  • listname.pop(element)
  • listname.remove()
  • listname.reverse()
  • listname.sort()
  • listname.clear()


(i) listname.append(element)

                    Adds an elements at the end of specified list and does not return any value.

Syntax:

listname.append(element)

Example

list=[1,2,3,4,5]

print("Before append", list)

list.append(6)

print("After append", list)

Output

Before append [1,2,3,4,5]

After append [1,2,3,4,5,6]

(ii) listname.count(element)

                    returns the number of occurrences of an element in the specified list.

Syntax

listname.count(element)

Example

list1=[1,2,3,4,5]

list.count(3)

list.count(6)

Output

1

0


(iii)listname.extend()

                    Appends the contents of second list to the first list and does not return any value.

Syntax

listname.extend(secondlist)

Example

list1=[1,2,3,4,5]

list2=[6,7,8]

print("Before extend", list1)

list1.extend(list2)

print("After extend", list1)

Output

Before extend [1,2,3,4,5]

After extend [1,2,3,4,5,6,7,8]


(iv) listname.insert()

                    Inserts the given elements at the given index in a specified list and does not return any value.

Syntax

listname.insert(index, element)

Example

list=[1,2,3,4,5]

print("Before insert", list)

list.insert(0,10)

print("After insert", list)

Output

Before insert [1,2,3,4,5]

After insert [10,1,2,3,4,5]


(v) listname.index()

                    Returns the elements form the end of the list.

Syntax

listname.index(element)

Example

list=[1,2,3,4,5]

print("index of 1 is", list.index(1))

print("index of 3 is", list.index(3))

Output

index of 1 is 0
index of 3 is 2


(vi) listname.pop()

                    Removes and returns the elements from the end of the list.

Syntax

listname.pop()

Example

list=[1,2,3,4,5]
print("Before pop", list)
print("The popped element is", list.pop())
print("After pop", list)

Output

Before pop [1,2,3,4,5]

The popped element is 5

After pop [1,2,3,4]


(vii) listname.pop(index)

                    Remove and returns the element at given index

syntax

listname.pop(index)

Example

list=[1,2,3,4,5]

print("Before pop", list)
print("The popped element is", list.pop(2))
print("After pop", list)
Output

Before pop [1,2,3,4,5]

The popped element is 3

After pop [1,2,4,5]


(viii) listname.remove()

                    Removes an element from the list and does not return any value.

Syntax

listname.remove(element)

Example

list=[1,2,3,4,5]

print("Before remove", list)

list.remove(3)

print("After remove", list)

Output

Before remove [1,2,3,4,5]

After remove [1,2,4,5]


(ix) listname.reverse()

                    Reverse the entire list.

Syntax

listname.reverse()

Example

list=[1,2,3,4,5]

print("Before reverse", list)

list.reverse()

print("After reverse", list)

Output

Before reverse [1,2,3,4,5]

After reverse [5,4,3,2,1]


(x) list.sort()

                    Sort the list in ascending order.

Syntax

listname.sort()

Example

list=[2,3,5,1,4]

print("Before sort", list)

list.sort()

print("After sort", [1,2,3,4,5])


List deletion

  • To remove a list element, del operator can be used if an element to be deleted is known.

Syntax

del listname[]       => deletes a single element
del listname[m:n] => deletes a range of element
del listname          => deletes the entire list

  • In the following code, the element 2 is deleted by mentioning its index within del operator.

Example

list=[1,2,3,4,5]
print("Before delete", list)
del list[1]
print("After delete", list)

Output

Before delete [1,2,3,4,5]
After delete [1,3,4,5]
  • The clear() method is used to empty a list.

Syntax

listname.clear()

Example

list=[1,2,3,4,5]
print("Before clear", list)
list.clear()
print("After clear", list)

Output

Before clear [1,2,3,4,5]
After clear []


Using built-in functions with list

  • len()
  • max()
  • min()
  • sum()


(i)len()

Returns the number of the elements on a list.

Example

list=[1,2,3,4,5]

print(len(list))

Output

5


(ii)max()

Returns the maximum value from the list

Example

list=[1,2,3,4,5]

print(max(list))

Output

5


(iii) min()

Returns the maximum value from the list

Example

list=[1,2,3,4,5]

print(min(list))

Output

1


(iv) sum()

Returns the sum of all the elements on a list.

Example

list=[1,2,3,4,5]

print(sum(list))

Output

15


List loop

The general way to traverse the elements of a list is for loop. The following code shows the use of for loop in accessing the elements of a list


Example

List=[1,2,3,4,5]

for i in List:

    print(i)

Output

1

2

3

4

5


The following code shows the use of range and len functions in accessing the indices of the elements of the list


Example

List=[1,2,3,4,5]

for i in range(len(list)):

    print i

Output

0

1

2

3

4


Here len is a function that returns the number of elements in the list and range is a function that returns a list of indices from 0 to n-1 where n is the length of the list.


Mutability

The list is a mutable datastructure. This means that its elements can be replaced, inserted and removed. A slice operator on the left of an assignment operation can update the list.


Example:

List=[1,2,3]

print(“Before mutation”, List)

List[0]=4

print(“After mutation”,list)

Output:

Before mutation [1,2,3]

After mutation [4,2,3]


Aliasing 

  • Since variables refers to objects, if we assign one variable to another, both variables refer to the same objects.
  • The same list has two different names, List1 and List2, we say that it is aliased. Changes made with one alias affect the other.


Example:

List1=[1,2,3]

List2=List1

print(List1 is List2)

List2[0]=5

print(list1)

Output

True

[5,2,3]


Cloning 

If we want to modify a list and also keep a copy of the original, we need to be able to make a copy of the list itself not just the reference. This is process is called cloning to avoid the ambiguity. The easiest way to clone a list is to use the slice operator.


Example:

List1=[1,2,3]

List2=List1[:]

print(List2)

List2[0]=5

print(List1)

Output:

[1,2,3]

[1,2,3]





No comments:

Post a Comment