Python                                                          Home : www.sharetechnote.com

 

 

 

 

Python - List

 

List in Python is similar to an array in any other language, but it has much more methods (features) to handle elements (item) of the list.

 

NOTE 1 : All the examples in this page are written in Python 3.x. It may not work if you use Pyton 2.x

NOTE 2 : All the examples in this page are assumed to be written/run on Windows 7 unless specifically mentioned. You MAY (or may not) need to modify the syntax a little bit if you are running on other operating system.

  • Creating a List : Empty List - Example 1
  • Creating a List : Numeric Items - Example 2
  • Creating a List : String Items - Example 3
  • Creating a List : 2D List - Example 4
  • Getting the length (size) of a List - Example 5
  • Accessing a List : Getting one item - Example 6
  • Accessing a List : Getting item from 2D List - Example 7
  • Accessing a List : Getting a block of items - Example 8
  • Accessing a List : Getting items in interval - Example 9
  • Accessing a List : Getting every other items - Example 10
  • Accessing a List : Looping Through a List : for - Example 11
  • Member check in a List - Example 12

 

Examples :

 

< Example 1 >

 

aList = []

print(aList)

 

Result :----------------------------------

[]

 

 

< Example 2 >

 

aList = [1, 2, 3, 4, 5]

print(aList)

 

Result :----------------------------------

[1, 2, 3, 4, 5]

 

 

< Example 3 >

 

aList = ["Item1","Item2","Item3"]

bList = ['Item1','Item2','Item3']

print(aList)

print(bList)

 

Result :----------------------------------

['Item1', 'Item2', 'Item3']

['Item1', 'Item2', 'Item3']

 

 

< Example 4 >

 

aList2D = [[1,2,3],[4,5,6]]

print(aList2D)

 

Result :----------------------------------

[[1, 2, 3], [4, 5, 6]]

 

 

< Example 5 >

 

aList = [1,2,3]

aList2D = [[1,2,3],[4,5,6]]

print(len(aList))

print(len(aList2D))

print(len(aList2D[0]))

 

Result :----------------------------------

3

2

3

 

 

< Example 6 >

 

aList = ["Item1","Item2","Item3"]

print(aList[2],aList[1],aList[0])

 

Result :----------------------------------

Item3 Item2 Item1

 

 

< Example 7 >

 

aList2D = [[1,2,3],[4,5,6]]

print(len(aList2D[0]))

print(len(aList2D[0][1]))

 

Result :----------------------------------

[1,2,3]

2

 

 

< Example 8 >

 

aList = [0,1,2,3,4,5,6,7,8,9]

print(aList[2:6])

 

Result :----------------------------------

[2, 3, 4, 5]

 

 

< Example 9 >

 

 

aList = [0,1,2,3,4,5,6,7,8,9]

print(aList[2:8:2])  # syntax is aList[start:stop:step]

 

Result :----------------------------------

[2, 4, 6]

 

 

< Example 10 >

 

aList = [0,1,2,3,4,5,6,7,8,9]

print(aList[::2])  

print(aList[2::2])

 

Result :----------------------------------

[0, 2, 4, 6, 8]

[2, 4, 6, 8]

 

 

< Example 11 >

 

aList = [0,1,2,3,4,5,6,7,8,9]

 

for i in aList :

   print("current i =",i)

 

Result :------------------------------------

 

current i = 0

current i = 1

current i = 2

current i = 3

current i = 4

current i = 5

current i = 6

current i = 7

current i = 8

current i = 9

 

 

< Example 12 >

 

fruits = ["Apple","BlueBerry","Banana","SrawBerry"]

 

print("'Apple' in fruits = ",("Apple" in fruits))

print("'Bean' in fruits = ",("Bean" in fruits))

 

 

Result :------------------------------------

 

'Apple' in fruits =  True

'Bean' in fruits =  False

 

 

 

< Example 13 >