Lists

Python Courses

Lists Example
Python supports collections known as lists. A list is defined using square brackets. Let us define as simple list:

c = [5,2,10,48,32,16,49,10,11,32,64,55,34,45,41,23,26,27,72,18]

In this case we have a list defined by the variable c. We defined a purely random set of numbers above.

Accessing elements

To access individual elements, we use the same brackets. To print the first element (Python starts counting from zero):

print(c[0])

To print the second element:

print(c[1])

To print the last element, you can count from the back using the minus sign.

print(c[-1])

Size of the list

You can get the length of the list using the len function. Example code:

c = [5,2,10,48,32,16,49,10,11,32,64,55,34,45,41,23,26,27,72,18]
print(len(c))

Datatypes

List may contain various types of variables in the same list including text, whole numbers, floating point numbers and so on.  For example, we may define a list with text variables:

fears = ["Spiders","Ghosts","Dracula"]
Functions
Line charts

4 thoughts on “Lists

  1. Reply
    Sarthak Mittal - June 26, 2016

    If I make a list which contains other lists, then how can I print elements of child-list.
    I am trying to use it as multidimensional list.

    1. Reply
      admin - July 22, 2016

      Use an index to print the child list: print(l[3])

  2. Reply
    Haritha Dharmadas - August 3, 2016

    how to accept a list of numbers as input

    1. Reply
      admin - August 6, 2016

      You can use:
      numbers = raw_input().split(“,”)
      print(numbers)

      Example input:
      1,5,7,3

Leave a Reply

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