Header Ads Widget

Responsive Advertisement

Lists in Python 3









Lists in Python3:



1.Lists are ordered sequence of mixed data types.

2.It can contain tuples, sets, dictionaries, lists, integer, float and boolean values.

3.Lists supports item assignments which makes them mutable also.

Examples of  valid lists:

1.mylist = [1,2,3,4]      #List of integers

2.mylist = [1,2,[3,4,100],8,9]   #Nested List

3.mylist = [1,2,3,True,12.50,'hii']   #List with integer, boolean, float and string values

4.mylist = [1,2,3,('hii',33,34,),{11,12,13},{'jan':1,'feb':2},['python', 'programming']]      #List of all                            datatypes and with all data structures including sets,tuples,dictionary and lists itself.

5.mylist = [ ]   #Empty list

Indexing and Slicing of List elements:

In list lis = [1,2,3,'disco'], indexing starts from 0th position to nth position which means the first element of the list is at 0th index, second element is at 1st index and last element is at (n-1)th index where 'n' is the total length or the total no of elements present inside a list.

E.g.   lis = [1,2,3,'disco']               #List containing integers and string values.

print(lis[0])                        #It will print the first element of the list i.e,1
print(lis[1])                        #It will print the second element of the list i.e,2

Slicing in List:
lis=[1,2,34,'True','False']       # List of integer and Boolean values
print(lis[:]) #It will print the whole list as it is.
print(lis[:3]) #It will print all the elements of the list till 2rd index.
print(lis[1:4]) #It will print all elements starting from index 1 to index 3.
print(lis[-3:-1]) #It will print all the elements from last 3rd to last 2nd.
print(lis[3:1]) #It will print a blank list like this []
output:
[1, 2, 34, 'True', 'False']
[1, 2, 34]
[2, 34, 'True']
[34, 'True']
[]

Negative indexing of List elements: 

It is possible to access list's elements by using negative indexes also but the difference is that in negative indexes we access elements from backward side.

E.g. lis = [1,2,3,'python']   #List containing integers and string values.

print(lis[-1])                       #It will print the last element of the list.i.e, python.
print(lis[-2])                       #It will print the last second element of the list i.e,3.
print(lis[-3])                       #It will print the last third element of the list i.e, 2.
print(lis[-4])                       #It will print the last fourth or 1st element of the list i.e, 1.

So, We can conclude that positive and negative indexes are very much similar to themselves but for ease of accessing the last elements ,using negative indices is a more convenient way.



Membership operator in Lists: Membership operators returns True and False based on the given situation, the two membership operators are:
                                                        1. 'in' operator
                                                        2. 'not in' operator
L=[12,3,'Guido','van',[1,2]]   # List containing integers and string values

print('Guido' in L) # Returns True
print([1,2] in L) # Returns True
print('Rossum' in L) # Returns False
L=[12,3,'Guido','van',[1,2]]   # List containing integers and string values

print('Guido' not in L) # Returns False
print('Rossum' not in L) # Returns True
Appending elements in an existing list:  By using append() we can add elements at last of an existing list as a list of elements.
E.g. 
L=[12,3,'james',[1,2]]
L.append([4,5]) # Adding list to our existing list.
print(L)
output:
[12, 3, 'james', [1, 2], [4, 5]]
Extending elements in lists:  By using extend() we can extend our existing list by adding elements at last.
E.g.
L=[12,3,'james',[1,2]]
L.extend([4,7]) # Adding list to our existing list.
print(L) #elements are added but not in form of lists.
output:
[12, 3, 'james', [1, 2], 4, 7]
del commmand in list:
E.g.

L=[12,3,'james',[1,2]]
del L[0] # deletes first element from list.
print(L)

Li=[12,11,'Guido','van','Rossum','Python']
del Li[0:3] # deletes from 0th index till 2nd index elements
print(Li)

output:
[3, 'james', [1, 2]]
['van', 'Rossum', 'Python']
pop function in lists: It will remove/deletes the last element and prints that deleted element as an output.
pop() contains only one argument.
E.g.

L=[12,3,'james',[1,2]]
print(L.pop())
print(L.pop(2))

output:
[1, 2]
james
Remove() in Lists:  remove() removes/deletes the specific element from list and takes only one argument.
E.g.
L=[12,3,'james',[1,2]]
L.remove('james')
print(L)
output:
[12, 3, [1, 2]]
Sort() in Lists:  By using sort() we can sort any given list in both the ways either in ascending or in descending order, and list.sort() will give always the same result as  list.sort(reverse=False) that is in ascending order. For List to be produced in descending order we should write list.sort(reverse=True).
E.g.
L=[12,3,4,10]
L.sort()
print(L)
L.sort(reverse=False)
print(L)
L.sort(reverse=True)
print(L)

A=['orange','strawberry','mango']
B=A.sort()
print(A)
print(B) #in sort() the value in which another value is assigned will always returns none.

output:
[3, 4, 10, 12]
[3, 4, 10, 12]
[12, 10, 4, 3]
['mango', 'orange', 'strawberry']
None
Sorted() in List:   Sorted() is used to overcome the problem which was generated in sort() as it produces none in last line of code, We can write as:

A=['orange','strawberry','mango']
B=sorted(A)
print(A)
print(B)

Output:
['orange', 'strawberry', 'mango']
['mango', 'orange', 'strawberry']


Thanks for reading






Post a Comment

1 Comments

  1. The concept is nicely explained in simple words thank you

    ReplyDelete