Monday, March 27, 2017

Lists in Python

Python Lists are implemented as arrays. The elements of the list can have any types. Let's work with a list example to see the various ways of interacting with it.

Let's create a list first. A list can be created by putting elements in the square brackets:

# A list of famous indian comics and their debut year. List can hold mix data types
>>> comics = ["Lot Pot", 1969, "Tinkle", 1980,  "Nagraj", 1986, "Doga", 1992, "Chacha Chaudhary", 1971]


List can be accessed as follows

>>> comics[2]

'Tinkle'

Indexing can be done backwards also

>>> comics[-2]
'Chacha Chaudhary'

A range of list elements can also be fetched

>>> comics[3:7]

[1980, 'Nagraj', 1986, 'Doga']

Getting the size of list

>>> len(comics)

10

Check if an element is part of list

# If the element is in list it will return True
>>> "Doga" in comics
True

#If the element is not in comics than return True. As "Doga" is in list, it returns False
>>> "Doga" not in comics
False

#If the element is not in list, it will return False
>>> "Rustam" in comics
False

>>> 

Looping the List

>>> for comic in comics:
...   print comic
... 
Lot Pot
1969
Tinkle
1980
Nagraj
1986
Doga
1992
Chacha Chaudhary

1971

List Concatenation

# Adding two list results with the elements of both list concatenated
>>> comics + ['Madhu Muskan', 1972]
['Lot Pot', 1969, 'Tinkle', 1980, 'Nagraj', 1986, 'Doga', 1992, 'Chacha Chaudhary', 1971, 'Madhu Muskan', 1972]

#Note the original list is still same. The concatenation returns a new list.
>>> comics

['Lot Pot', 1969, 'Tinkle', 1980, 'Nagraj', 1986, 'Doga', 1992, 'Chacha Chaudhary', 1971]

Deleting elements in list

#Deleting a range of elements
>>> del comics[2:6]

>>> comics
['Lot Pot', 1969, 'Doga', 1992, 'Chacha Chaudhary', 1971]

Interesting Operations

Fetching years of publication only

>>> comics_years = [comic for comic in comics if type(comic) == int]
>>> comics_years

[1969, 1980, 1986, 1992, 1971]


#Find publication years greater than 1985
>>> [year for year in comics_years if year > 1985]
[1986, 1992]

Putting the function in parenthesis creates a generator function. Generators are iterators which generate the value on demand

>>> years = (year for year in comics_years if year > 1985)
>>> 
>>> years
<generator object <genexpr> at 0x1020e7c30>

#List out the values using generator
>>> list(years)
[1986, 1992]

#Once the value are generated the generator does not returns anything more
>>> list(years)

[]

Cloning List

>>> comics
['Lot Pot', 1969, 'Tinkle', 1980, 'Nagraj', 1986, 'Doga', 1992, 'Chacha Chaudhary', 1971]

#Making an alias
>>> comics_alias = comics

#Making a deep copy
>>> comics_clone = comics[:]
>>> del comics[0:2]

#alias reflects the change
>>> comics_alias
['Tinkle', 1980, 'Nagraj', 1986, 'Doga', 1992, 'Chacha Chaudhary', 1971]

#clone has no impact
>>> comics_clone
['Lot Pot', 1969, 'Tinkle', 1980, 'Nagraj', 1986, 'Doga', 1992, 'Chacha Chaudhary', 1971]


Nested List

Lists can be nested. In fact, the comics representation can be made nested as that's a more logical representation of data.

>>> comics = [['Lot Pot', 1969], ['Tinkle', 1980], ['Nagraj', 1986], ['Doga', 1992], ['Chacha Chaudhary', 1971]]
>>> comics
[['Lot Pot', 1969], ['Tinkle', 1980], ['Nagraj', 1986], ['Doga', 1992], ['Chacha Chaudhary', 1971]]

>>> for comic in comics:
...   print comic
... 
['Lot Pot', 1969]
['Tinkle', 1980]
['Nagraj', 1986]
['Doga', 1992]
['Chacha Chaudhary', 1971]

Nesting can be used to handle matrix representation.  To access any element we can use the nested index i,j

>>> comics[1][1]
1980

The nesting can be any deep and can be arbitrary.

No comments:

Post a Comment