Programming in Python : Lists

The list data type in python allows more operations compared to the other data types. The items in the lists are enclosed by brackets ‘[]’. It may contain numbers, srtings, tuples or lists (nesting of lists).

>>> list1 = [1,2,3,’how’,’are’,’you’]

>>> list1
[1, 2, 3, ‘how’, ‘are’, ‘you’]

>>> list1[3]

>>> list1[3:]
[‘how’, ‘are’, ‘you’]

>>> list1[:2]
[1, 2]

>>> list1[:2] = ‘and’

>>> list1
[‘a’, ‘n’, ‘d’, 3, ‘how’, ‘are’, ‘you’]

>>> list1[:2] = [‘and’]

>>> list1
[‘and’, 3, ‘how’, ‘are’, ‘you’]

>>> list1 = [12,34,56,’s’,’y’]

>>> list1
[12, 34, 56, ‘s’, ‘y’]

>>> list2 = [22,’22’]

We can add members of one list to the members of another list.
>>> list1[3:3] = list2

>>> list1
[12, 34, 56, 22, ’22’, ‘s’, ‘y’]

>>> list3 = [33,list2,’33’]

The following list is nested.
>>> list3
[33, [22, ’22’], ’33’]

>>> list3[1][1]

>>> list3[1][0]

The del statement :

It is used to remove an element from a list when the index of that element is given. It can be used to clear the entire list or remove the variable.

Author: Abhilash

Hey! My name is Abhilash - A Ruby developer for years, specialised on web programming, working as a freelancer. Mainly working on Ruby On Rails platform since 2010. This blog is about Ruby, Ruby On Rails and other subjects that I work. You can contact me here: Abhilash AK

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s