Working with Python Lists

  • A list is a data-structure that can be used to store multiple data at once.
  • The list is ordered, mutable (changeable after it is created) and the elements are indexed according to a sequence and the indexing is done with 0 as the first index, the second index is one, and so forth.
  • List allows duplicate members
  • Lists are written as a list of comma-separated values between square brackets [ ].

Creating, Accessing, Indexing, Deleting, Slicing and List Operations

# List with Same Data Type
myList = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
print(myList)
['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']

# List with Different Data Types
myListDiffValues = ["Apple","Orange","Mango", 26, 54, 108, True, 98.5]
print(myListDiffValues)
['Apple', 'Orange', 'Mango', 26, 54, 108, True, 98.5]

# Lists Containing Lists
myListContainsLists = [["Sanskrit"],["Hindi"], ["Telugu"], ["Kannada","Malayalam"], "Tamil","English"]
print(myListContainsLists)
[['Sanskrit'], ['Hindi'], ['Telugu'], ['Kannada', 'Malayalam'], 'Tamil', 'English']

# Identifying Data Type
type(myListContainsLists[0])
list

# Accessing List with Index

myListContainsLists[0]
['Sanskrit']

myListContainsLists[1]
['Hindi']

myListContainsLists[2]
['Telugu']

myListContainsLists[3]
['Kannada', 'Malayalam']

myListContainsLists[4]
'Tamil'

myListContainsLists[5]
'English'

# Negative Indexing - Python allows negative indexing for its sequences. The index of -1 refers to the last item, -2 to the second last item and so on.
>>> myList = ['G','I','R','I','S','H']
>>> print(myList)
['G', 'I', 'R', 'I', 'S', 'H']
>>> print(myList[-1])
H
>>> print(myList[-6])
G

#Lists Containing Tuples
myListContainsTuples = [("Sanskrit"),("Hindi"), ("Telugu"), ("Bengali", "Gujarati"),["Kannada","Malayalam"], "Tamil","English"]
print(myListContainsTuples)
['Sanskrit', 'Hindi', 'Telugu', ('Bengali', 'Gujarati'), ['Kannada', 'Malayalam'], 'Tamil', 'English']

myListContainsTuples[0]
'Sanskrit'

# Identifying Data Type
type(myListContainsTuples[3])
tuple

# Slicing the Lists
[start : stop : steps]  
Slicing will start from index start will go up to stop in step of steps. 
Default value of start is 0, stop is last index of list and for step it is 1 

>>> myList = ['G','I','R','I','S','H']
>>> print(myList[2:5]) # elements 3rd to 5th
['R', 'I', 'S']
>>> print(myList[:-5]) # elements begining to 4th
['G']
>>> print(myList[4:]) # elements 5th to end
['S', 'H']
>>> print(myList[:]) # elements begining to end
['G', 'I', 'R', 'I', 'S', 'H']

>>> myList = [1,2,3,4,5,6,7,8,9,10]
>>> myList
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> myList1_5 = myList[1:5]
>>> myList1_5
[2, 3, 4, 5]

>>> myList1_ = myList[1:]
>>> print(myList1_)
[2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> myList_6 = myList[:6]
>>> print(myList_6)
[1, 2, 3, 4, 5, 6]

>>> myList1_7_2 = myList[1:7:2]
>>> print(myList1_7_2)
[2, 4, 6]

>>> myList_rev = myList[ : :-1]
>>> print(myList_rev)
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

>>> myList_rev_9_5_2 = myList[9:4:-2]
>>> print(myList_rev_9_5_2)
[10, 8, 6]

# del keyword - Removes the specified index, also deletes the list completely
>>> myList = ["Sunday","Monday","Tuesday"]
>>> del myList[0]
>>> print(myList)
['Monday', 'Tuesday']

>>> myList = ["Sunday","Monday","Tuesday"]
>>> print(myList)
['Sunday', 'Monday', 'Tuesday']
>>> del myList
>>> print(myList)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'myList' is not defined

# Basic List Operations
1) Looping through a List or Iteration
2) Length
3) "in" Keyword (Membership Operator)
4) Concatination
5) Repetition

# Looping through a List
>>> myList = ["Girish","Sanjay","Ramesh"]
>>> for x in myList:
...     print(x)
...
Girish
Sanjay
Ramesh
>>> myList = ['G','I','R','I','S','H']
>>> for x in myList:
...     print(x)
...
G
I
R
I
S
H
>>> myList = [2,4,6,8,10]
>>> for x in myList:
...     print(x)
...
2
4
6
8
10

# len() - determines how many items a list has
>>> myList
[2, 4, 6, 8, 10]
>>> len(myList)
5
>>> myList = ['G','I','R','I','S','H']
>>> len(myList)
6
>>> myList = ["Girish","Sanjay","Ramesh"]
>>> len(myList)
3

# "in" keyword - To check if a specified item is present in a list or not
>>> myList
['Girish', 'Sanjay', 'Ramesh']
>>> if "Girish" in myList:
...     print("Yes, 'Girish' is in myList")
...
Yes, 'Girish' is in myList

# Concatination
>>> lst1 = [1,2,3]
>>> lst2 = [4,5,6]
>>> print(lst1 + lst2)
[1, 2, 3, 4, 5, 6]

# Repetition
>>> lst1*4
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> lst3 =["Hello"]
>>> lst3*3
['Hello', 'Hello', 'Hello']
>>> 6 in lst2
True

List Methods

  • append()
  • clear()
  • copy()
  • count()
  • extend()
  • index()
  • insert()
  • pop()
  • remove()
  • reverse()
  • sort()
#  append() - Adds an element at the end of the list
>>> myList = ["Sunday","Monday","Tuesday"]
>>> myList.append("Wednesday")
>>> print(myList)
['Sunday', 'Monday', 'Tuesday', 'Wednesday']

# insert() - Add an item at the specified index
>>> myList = ["Sunday","Monday","Tuesday"]
>>> myList.insert(2,"Wednesday")
>>> print(myList)
['Sunday', 'Monday', 'Wednesday', 'Tuesday']

# remove() - Removes the specified item
>>> myList = ["Sunday","Monday","Tuesday"]
>>> myList.remove("Monday")
>>> print(myList)
['Sunday', 'Tuesday']

# pop() - Removes the specified index, or the last item if index is not specified
>>> myList = ["Sunday","Monday","Tuesday"]
>>> myList.pop(1)
'Monday'
>>> myList
['Sunday', 'Tuesday']
>>> myList.pop(0)
'Sunday'
>>> myList
['Tuesday']
>>> myList = ["Sunday","Monday","Tuesday"]
>>> myList.pop()
'Tuesday'
>>> print(myList)
['Sunday', 'Monday']

# clear() - Empties the List
>>> myList = ["Sunday","Monday","Tuesday"]
>>> myList.clear()
>>> print(myList)
[]

# count() - Returns the number of elements with the specified value
>>> myList = ["Sunday","Monday","Tuesday","Sunday"]
>>> x = myList.count("Sunday")
>>> print(x)
2

>>> myListNum = [2,4,6,4,8,4,10, 12, 4]
>>> y = myListNum.count(4)
>>> print(y)
4

# copy() - Returns a copy of the specified list.
>>> myList = ["Sunday","Monday","Tuesday"]
>>> myListCp = myList.copy()
>>> print(myListCp)
['Sunday', 'Monday', 'Tuesday']

# extend() - Adds the specified list elements to the end of the current list.
>>> myList = ["Sunday","Monday","Tuesday"]
>>> myList2 = ["Wednesday","Thursday","Friday","Saturday"]
>>> myList.extend(myList2)
>>> print(myList)
['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']

# reverse() - Reverses the sorting order of the elements.
>>> myList = ["Sunday","Monday","Tuesday"]
>>> myList.reverse()
>>> print(myList)
['Tuesday', 'Monday', 'Sunday']

# sort() - sorts the list ascending 
>>> myList = ["Girish","Sanjay","Ramesh"]
>>> myList.sort()
>>> print(myList)
['Girish', 'Ramesh', 'Sanjay']

# index() - returns the position at the first occurrence of the specified value.
>>> myList = [22, 44, 66, 88, 108, 44]
>>> x = myList.index(44)
>>> print(x)
1

References

  • https://docs.python.org

Learn more about Python Advanced features in the next Blog Articles.

Happy Learning!