login as: sloba
sloba@*********'s password:
Welcome to Linux Mint 17.2 Rafaela (GNU/Linux 3.16.0-38-generic x86_64)
Welcome to Linux Mint
* Documentation: http://www.linuxmint.com
Last login: Mon Jul 13 14:18:53 2015 from 172.27.66.246
sloba@sloba-VirtualBox ~ $ python
Python 2.7.10 (default, Jul 13 2015, 14:15:32)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> list =[1,2,3,4]
>>> list
[1, 2, 3, 4]
>>>
>>> print list
[1, 2, 3, 4]
>>>
>>>
Let's see an example to have a list having different data types :
sloba@sloba-VirtualBox ~ $ python
Python 2.7.10 (default, Jul 13 2015, 14:15:32)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> list1 = [1, 2,A,B]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'A' is not defined
>>>
sloba@sloba-VirtualBox ~ $ python
Python 2.7.10 (default, Jul 13 2015, 14:15:32)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> list1 = [1, 2,A,B]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'A' is not defined
>>>
>>> list1 = [1, 2,'A','B']
>>> list1
[1, 2, 'A', 'B']
>>>
>>> list
[1, 2, 3, 4]
>>> list1
[1, 2, 'A', 'B']
>>> list + list1
[1, 2, 3, 4, 1, 2, 'A', 'B']
>>>
>>> indexlist
[1, 2, 3, 'AA', 'BB', 4]
>>>
>>> indexlist[2:]
[3, 'AA', 'BB', 4]
>>> indexlist[1:4]
[2, 3, 'AA']
We are getting "AA" as the fourth values because the index starts from "0" i.e. zero.
>>> indexlist[0]
1
>>>
>>> reversedlist = indexlist[::-1]
>>> reversedlist
[4, 'BB', 'AA', 3, 2, 1]
>>> reversedlist
[4, 'BB', 'AA', 3, 2, 1]
>>> reversedlist.reverse()
>>> for i in reversedlist : print i
...
1
2
3
AA
BB
4
Now let's see if we want to repeat the values two times .
>>> a =[1,2]
>>> a
[1, 2]
>>> a *2
[1, 2, 1, 2]
>>> a =[1,2,3]
>>> a
[1, 2, 3]
>>> a[0]
1
We see the starting value [index starting from 0 ] of the list "a" is one i.e. "1" , now let us update to four i.e. '4'
>>> a
[1, 2, 3]
>>> a[0] =4
>>> a
[4, 2, 3]
>>>
>>> a
[4, 2, 3]
>>> a.append(5)
>>> a
[4, 2, 3, 5]
>>>
>>> list
['A', 'B', 'C']
>>> list.extend(['D','E'])
>>> list
['A', 'B', 'C', 'D', 'E']
>>>
>>> list
['A', 'B', 'C', 'D', 'E']
>>> list.insert(1,"insert")
>>> list
['A', 'insert', 'B', 'C', 'D', 'E']
>>>
>>> list
['A', 'insert', 'B', 'C', 'D', 'E']
>>> list.remove("insert")
>>> list
['A', 'B', 'C', 'D', 'E']
>>>
>>> list
['A', 'B', 'C', 'D', 'E']
>>>
>>>
>>> list.append('A')
>>> list
['A', 'B', 'C', 'D', 'E', 'A']
>>> list.remove("A")
>>> list
['B', 'C', 'D', 'E', 'A']
>>>
>>> list
['B', 'C', 'D', 'E', 'A']
>>> list.pop()
'A'
>>> list
['B', 'C', 'D', 'E']
>>>
From the above we see that this removes the last element but also return the value which was removed from the list.
>>> list =['A', 'B', 'C', 'D', 'E']
>>> list
['A', 'B', 'C', 'D', 'E']
>>> len(list)
5
>>>
>>> list
['A', 'B', 'C', 'D', 'E']
>>> list.count('A')
1
>>>
>>> list
['A', 'B', 'C', 'D', 'E']
>>> list.reverse()
>>> list
['E', 'D', 'C', 'B', 'A']
>>>
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)