Lists in Python | Part 1

05 min read

Lists act as a basic data structure in Python. A list can be defined as a mutable, ordered sequence of elements. The elements inside the list are also addressed as items in general. In Python, the items in the list are defined within quotes of [] square brackets. Lists pose a greater advantage to the developer when large samples of a similar kind are addressed in the program. Lists allow the user to reduce the size of the code and also apply a single operation on all the elements inside the list in one line.

It is important to note that individual characters inside a string cannot be manipulated, for example 

x= 'string'
print(x[2]) # results in ERROR
x[3] = y # results in ERROR

But a list provides an oppurtunity to edit its elements. 'string' can be converted into ['s', 't', 'r', 'i', 'n', 'g'] and then above operations can be applied on it. Hence list can be defined as mutable set of elements. To understand better let us consider a list with few strings,

Success = [ 'hard_work', 'dedication' , 'punctual', 'discipline']

print(Success)
[ 'hard_work', 'dedication' , 'punctual', 'discipline']



Indexing the elements

The index of elements starts from 0 and goes up to 'n' incrementing one for n+1 elements.

  • print(Success[2])     # returns punctual
  • print(Success[32])     # returns "IndexError: list index out of range" since that index is not available in defined list.
  • We can also use negetive index which counts from -n to the first element to -1 for the last element.
  • print(Success[-2]) # returns puctual
  • print('you need to have_' + Success[-3] + '_to be successful')  # returns "you need to have_dedication_to be succesful

 

Adding new elements to the list

Success[1] = 'hygenic'  # adding the element in the index we want to pushing other elements eventually to the next index.
print(Success)

Output:
[ 'hard_work', 'hygenic', 'dedication' , 'punctual', 'discipline']
By append() method

Success.append('workout') # can also be added as Success.insert(4,'workout') to insert workout in 4th position
print(Success)

Output:

[ 'hard_work', 'hygenic', 'dedication' , 'punctual', 'discipline', 'workout']

 

Deleting an element from the list

del Success[1]    # hygenic deleted                    # pop command can also be used to delete elements from a list at a specified index.
print(Success)

output:
[ 'hard_work', 'dedication' , 'punctual', 'discipline', 'workout']



Printing only a section of elements in a list

consider a list of elements 

a = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

Now, to access only the first 4 elements we write as:
print(a[:5])

Output: ['1', '2', '3', '4']

To print elements in steps of 2. Or to print all odd numbers in the above list we can write the Python code as:

print(a[0:9:2]) # from 0th index to 9th index at steps of 2.

Output: ['1', '3', '5', '7', '9']

or if the elements are addressed from 0th ['1', '3', '5', '7', '9']

POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error