Tuesday, 25 June 2019

List

a=10
a=20
b=20.45
b=23.56
c=2+3j
c=4+4j
d="raj"
d="sita"
e=True
e=False

above all are basic data types which store only one value. To get the old and new value we have to go to collections:

List:

The list is nothing but a collection of elements (items) inside square brackets, separated by commas.
A list may have a different kind of types (int, float, bool, complex, string) i.e Heterogeneous.

Syntax:
variable=[ elements]

See the below Examples:
a=[10,2.5,True,2+3j,"Ravi"]

a=[10,20,30,40]#collection of items like arrays
a=[10,-20,12.5,'Ravi',"Sita"] #Different types like objecr
a=[10,-20,10]  #duplication

Lists in Python is just like Arrays in Java but in arrays are the same type whereas in Lists its different types.

How to access the List items?

We can use the index operator [] to access an item in a list. Index starts from 0(zero). So, If a list have 5 elements will have index from 0 to 4 (size-1). or by using the negative index also we can access

Trying to access an element other that this will raise an IndexError. The index must be an integer. We can't use float or other types, this will result into TypeError.

Ex:

>>> a=[10,20,30]
>>> print(a[1])
20

# elements from beginning to end
>>>print(a[:])
>>>[10,20,30]

Other example:

>>> print(a[1])
>>> 20
>>> print(a[-1])
>>> 30

Nested Lists:

List in a list is called Nested List. Nested list can be accessed using nested indexing.

Example:
>>> a=["python",[10,20,30]]
>>> print(a)
['python', [10, 20, 30]]
>>>
>>> a[0][0]
'p'
>>> a[0][1]
'y'
>>> a[0][2]
't'
>>> a[0][3]
'h'
>>> a[0][4]
'o'
>>> a[0][5]
'n'
>>> a[0][6]
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    a[0][6]
IndexError: string index out of range
>>> a[1][0]
10
>>> a[1][1]
20
>>> print(a[0])
python
>>> print(a[1])
[10,20,30]


Examples:
>>> a=[10,20,30]
>>> print(a)
[10, 20, 30]
>>> a=[10,20,10,30]
>>> print(a)
[10, 20, 10, 30]
>>> a[0]=40
>>> print(a)
[40, 20, 10, 30]
>>> a=[40,20,2.5,True,"Ravi",2+3j]
>>> 
>>> a=[10,20,30,40]
>>> a[-1]
40
>>> a[1:4]
[20, 30, 40]
>>> a[4] #Error because there is not 4th index in the list
>>> a=[10,20,True]
>>> a[0]="Ravi"
>>> print(a)
['Ravi', 20, True]
>>> 

Addition or extending the list

append()
This function is used to append the items in the list.
Ex:

>>> a=[10,20,30]
>>> print(a)
>>> [10,20,30]
>>> a.append(15)
>>>print(a)
>>>[10,20,30,15]

Ex2:
a=[2,3,4] # list is created
>>> append(5) # 5 is added at the end of the list
>>> a.append(a[0]) # index a[0] is added at the end of the                    #list
>>> a
[2, 3, 4, 5, 2]

extend()

 This method is used to add the elements(objects) to the existing list.
Ex:
>>> a=[2,3,4]
>>> a
[2,3,4]
>>> a.extend([7,8,5])
>>> print(a)
[2, 3, 4, 7, 8, 5]

>>> 

+ operator is used to combine two list:
>>> b=[2,3,4]
>>> print(b)
[2, 3, 4]
>>> print(b+[9,6,4])
[2, 3, 4, 9, 6, 4]
By using we can combine already existing two lists:
>>> print(a+b)
[2, 3, 4, 7, 8, 5, 2, 3, 4]
>>> 

insert()method
It is used to insert one item at a desired location.

Ex:
>> c=[2,4,7]
>>> c.insert(1,5) # insert at index 1 and value to be inserted is 5
>>> print(c)
[2, 5, 4, 7]
>>> 

Syntax:

Listname.insert(index no,value)
Ex:
>>> a=[2,3,4,"ravi"]
>>> b,c,d,e=a
>>> print(b)
2
>>> print(c)
3
>>> print(e)
ravi
>>> print(d)
4
>>>
Deletion items in a list:

remove(): 

It is used to deleting the first matched value, not a specific index.
Ex:
>>> a=[2,3,4,5]
>>> a.remove(4)
>>> a
[2, 3, 5]
>>> b=[2,4,5,2]
>>> b.remove(2)
>>> print(b)
[4, 5, 2]
>>> 

del

It is used to delete item at specific index:
Ex:
>>> b=[4,5,2]
>>> print(b)
[4, 5, 2]
>>> del b[1]
>>> print(b)
[4, 2]

To delete the entire list we can use del.
Ex:
>>> del a

>>> print(a)

pop():

It deletes the item at a specific index and returns it.
Ex:
>>> a=[2,3,6,8,9]
>>> a.pop()
9
>>> a.pop(3)
8
>>> 

Key points in List:

1. List is a ordered list
Ex: a=[10,20,30]

2. Duplicates are allowed
Ex: a=[10,20,20,30]

3. Heterogeneous (Different types of data types)
Ex: a=[10,’ravi’,True,10.2,2+3j]

4. Mutable (We can modify the data)
a=[10,20,20,30]
print(a):-> [10,20,20,30]
a[1]=50
print(a):-> [10,50,20,30]

5. List is dynamically growable.
Ex:
a=[10,20,20,30]
a.append(“Sita”)
a.append(True)
print(a): [10,20,20,30,”Sita”,True]

6. We can delete or remove the items from the list
>>> print(a.pop(1))
20
>>> del a[2]
>>> print(a)
['Ravi', True, (2+3j)]
>>> 

Assignments:


1. Which of the following commands will create a list?
a) a = list()
b) a = [].
c) a = list([1, 2, 3])
d) all of the mentioned


Ans(d)
2. What is the output when we execute list('rajendra')?
a) ['r', 'a', 'j', 'e', 'n', 'd', 'r', 'a']
b) [‘rajendra’].
c) [‘dra’].
d) [‘ardnejar’].
Ans: (a)



Previous Topic:  Immutable

You may like the following posts:

     type casting
    
         Ordered

No comments:

Post a Comment

Files with Exception handling

#Ask the user to take two input integer values var a,b try:     a=int(input("enter any integer value:"))     b=int(input(&qu...