Lists in Python-DecodingDevOps

Lists in Python-DecodingDevOps

The list is one of the popular data structure in python which contains a sequence of items separated by commas and are enclosed within the square brackets. it can contain heterogeneous values such as integers, floats, strings. Lists indexes start from 0 and end with -1 and it works as a sequence. Python lists are mutable that is we can modify or change the elements in the list. Duplicates are allowed and order is important in the list. The list is sortable and it grows and shrinks size as needed.

# creation of the list

list1 = ['apple','banana','orange','mango','guava']</pre>

# get the first element in the list

list1[0]                  # output is apple which is at first index

list1[1]                  # output is banana which is at second index

list1[6]                  # results in index error as there is no 6 th element

to find the length of the list we use the len() function.
list1 = [ 'apple','banana','orange','mango','guava']

len(list1)
OUTPUT :
5

METHODS IN LISTS: let's check some of the important methods in the lists:

1. list. append(element): This method add another element at the end of the list.

2.list.insert(index, element): This method adds the element at the particular index given.

3. list.extend(another_list): This method will add the elements of list 2 at the end of the list.

4. list.index(element): This method will give the index position of the element.

5. list.remove(element): This method will search for the first occurrence of  element and removes it.

6. list.pop(): This method will remove the last element of the list.

7. list.sort(): This method will sort out the list in place.

8. list.reverse(): This method will reverse the list in the place.

9. list.count(): This method returns the number of times the item appears in the list.

 

# create a list
list1 = [ 'apple','banana','orange','mango','guava']

#using append method
list1.append("strawberry")

#using insert method
list1.insert(1,"strawberry")

#using extend method
list2=['ram','ravi','hari']
list1.extend([list2])

#using index method
list1.index("banana")

#using remove method
list1.remove('apple')

#using pop method
list1.pop()

#using sort method
list3=[4,5,3,1,2]
list3.sort()

#using reverse method
list1.reverse()

#using count method
list1.count()

OUTPUT:

1. list1 = [ 'apple','banana','orange','mango','guava','strawberry']

2. list1 = [ 'apple','strawberry','banana','orange','mango','guava']

3. list1 = [ 'apple','banana','orange','mango','guava','ram','ravi','hari']

4. 1 

5. list1 = [ 'apple','orange','mango','guava']

6. list1 = [ 'apple','banana','orange','mango']

7. [1,2,3,4,5]

8.list1 = ['guava','orange','mango','banana','apple']

9. 5

Using for and in loops: 

for index, name in enumerate(list1):
         print("Index is %s for list1: %s" % (index, name)

SLICING: Extracting a part from the list using the slicing operator [ : ] .

list = ['a', 'b', 'c', 'd']
 
print list[1:-1]            ## ['b', 'c']
 
list[0:2] = 'z'            ## replace ['a', 'b'] with ['z']
 
print list                 ## ['z', 'c', 'd']

 

 

 

 

                                                                                                                                   

 

Leave a Reply

Your email address will not be published. Required fields are marked *