Lists

  • List items are ordered, changeable, and allow duplicate values.
  • List items are indexed, the first item has index [0], the second item has index [1] etc

Creating a List

In [4]:
myList = ['Apple', 'Banana', 'Orange', 'Data', 'Test']

print(myList)
['Apple', 'Banana', 'Orange', 'Data', 'Test']
In [5]:
# printing each element
for i in myList:
    print(i)
Apple
Banana
Orange
Data
Test

Another way to Create a list

In [6]:
myList1 = list()  # Empty List
print(myList1)
[]

Adding Elements

In [7]:
myList1.append('One')
myList1.append('Two')
print(myList1)
['One', 'Two']
In [8]:
# Creating an List by direct assignment
myList2 = list([2.2, 'One', 3, 'Integer'])
print(myList2)
[2.2, 'One', 3, 'Integer']

Selecting an element by index value

In [8]:
print(myList2[0])
2.2
In [9]:
print(myList2[2])
3

Out of Index Error
List index starts from 0 till n-1 where n is the no of element in the list

In [10]:
print(myList2[4])
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-10-82396407b7b4> in <module>
----> 1 print(myList2[4])

IndexError: list index out of range

Slicing
Way to select or slice element from list

In [12]:
# Select all element from index 2 to end
print(myList[2:])   
['Orange', 'Data', 'Test']
In [13]:
# Get last element 
# Index nos will start from -1 to -n in backward list where n is the no of elements in list
print(myList[-1])
Test
In [14]:
# Get element from index 2 except the last one
print(myList[2:-1])
['Orange', 'Data']
In [15]:
# Reversing the whole list
print(myList[::-1])
['Test', 'Data', 'Orange', 'Banana', 'Apple']

Element Exists??

In [16]:
['Test' in myList]
Out[16]:
[True]
In [17]:
['Mango' in myList]
Out[17]:
[False]
In [18]:
'Banana' in myList
Out[18]:
True

No of elements in list or lenght

In [19]:
len(myList)
Out[19]:
5

Manupulating a List

In [20]:
# Appending an element 0
myList.append(0)
print(myList)
['Apple', 'Banana', 'Orange', 'Data', 'Test', 0]
In [21]:
# Inserting an element at index 1 (2nd element)
myList.insert(1, '2')
print(myList)
['Apple', '2', 'Banana', 'Orange', 'Data', 'Test', 0]
In [22]:
# Removing the last element
last = myList.pop()
print(last)
print(myList)
0
['Apple', '2', 'Banana', 'Orange', 'Data', 'Test']
In [23]:
# Removing the element 2
item = myList.remove('2')
print(item)
print(myList)
None
['Apple', 'Banana', 'Orange', 'Data', 'Test']
In [24]:
# Removing element which does not exists
item = myList.remove('Pop')
print(item)
print(myList)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-24-44aa9d2032c4> in <module>
      1 # Removing element which does not exists
----> 2 item = myList.remove('Pop')
      3 print(item)
      4 print(myList)

ValueError: list.remove(x): x not in list
In [25]:
# Clearing the whole list 
myList2.clear()   # can be done as re-initialization the variable -->  myLList2 =[]
print(myList2)
[]
In [26]:
myList.reverse()   # inplace reverse
print(myList)
['Test', 'Data', 'Orange', 'Banana', 'Apple']
In [27]:
myList.sort()    # inplace sort
print(myList)
['Apple', 'Banana', 'Data', 'Orange', 'Test']
In [32]:
list2 = myList.copy()      # creating a copy
print("list2 before --> ",list2)
list2.reverse()
print("list2 after  --> ",list2)
print("myList --> ",myList)
list2 before -->  ['Apple', 'Banana', 'Data', 'Orange', 'Test']
list2 after  -->  ['Test', 'Orange', 'Data', 'Banana', 'Apple']
myList -->  ['Apple', 'Banana', 'Data', 'Orange', 'Test']
In [33]:
list3 = sorted(list2)      # Create another copy
print("list2 --> ",list2)
print("list3 --> ",list3)
list2 -->  ['Test', 'Orange', 'Data', 'Banana', 'Apple']
list3 -->  ['Apple', 'Banana', 'Data', 'Orange', 'Test']
In [31]:
# clearing all the list
myList=[]
myList2=[]
list2=[]
list3=[]

Concat

In [34]:
list2 = ['this', 'is', 'another', 'data']
list3 = ['which', 'we', 'are', 'going', 'to', 'manipulate']
In [36]:
print("list2 --> ", list2)
print("list3 --> ", list3)

myList = list2 + list3
print(myList)
list2 -->  ['this', 'is', 'another', 'data']
list3 -->  ['which', 'we', 'are', 'going', 'to', 'manipulate']
['this', 'is', 'another', 'data', 'which', 'we', 'are', 'going', 'to', 'manipulate']
In [37]:
# Print all elements
print(myList[:])
['this', 'is', 'another', 'data', 'which', 'we', 'are', 'going', 'to', 'manipulate']
In [38]:
# Get element from index 4 till 6
print(myList[4:7])
['which', 'we', 'are']
In [39]:
# Get element from 0 to 1
print(myList[:2])
['this', 'is']
In [40]:
# Get element from 0 to 6 with 2 hops/steps
print(myList[:7:2])
['this', 'another', 'which', 'are']
In [41]:
# Get all element with 4 hops
print(myList[::4])
['this', 'which', 'to']
In [42]:
# Get all element with 4 hops but backward
print(myList[::-4])
['manipulate', 'we', 'is']
In [43]:
# Reverse the whole list
print(myList[::-1])
['manipulate', 'to', 'going', 'are', 'we', 'which', 'data', 'another', 'is', 'this']

Copy List

In [44]:
list4 = list2           # Copy be reference
list5 = list2.copy()    
list6 = list2[:]

print("list2 --> ", list2)
print("list4 --> ", list4)
print("list5 --> ", list5)
print("list6 --> ", list6)
list2 -->  ['this', 'is', 'another', 'data']
list4 -->  ['this', 'is', 'another', 'data']
list5 -->  ['this', 'is', 'another', 'data']
list6 -->  ['this', 'is', 'another', 'data']
In [45]:
list2[1] = 'are'   # altering list2 2nd element

print("list2 --> ", list2)
print("list4 --> ", list4)
print("list5 --> ", list5)
print("list6 --> ", list6)
list2 -->  ['this', 'are', 'another', 'data']
list4 -->  ['this', 'are', 'another', 'data']
list5 -->  ['this', 'is', 'another', 'data']
list6 -->  ['this', 'is', 'another', 'data']
In [47]:
list2.append('extra')

print("list2 --> ", list2)
print("list4 --> ", list4)
print("list5 --> ", list5)
print("list6 --> ", list6)
list2 -->  ['this', 'are', 'another', 'data', 'extra', 'extra']
list4 -->  ['this', 'are', 'another', 'data', 'extra', 'extra']
list5 -->  ['this', 'is', 'another', 'data']
list6 -->  ['this', 'is', 'another', 'data']

List Comprehension

In [54]:
# List Comprehension

arr = list(range(1,20))
print(arr)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
In [55]:
# Creating another list from arr
arr2 = [ i*i for i in arr]
print(arr2)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361]
In [57]:
# Fetch only even value via comprehention
[i for i in arr if i%2==0 ]
Out[57]:
[2, 4, 6, 8, 10, 12, 14, 16, 18]
In [64]:
[i if i%2==0 else 0 for i in arr ]
Out[64]:
[0, 2, 0, 4, 0, 6, 0, 8, 0, 10, 0, 12, 0, 14, 0, 16, 0, 18, 0]
In [ ]: