Python: Lists (difference between Lists and Tuples)

#Lecture 6 Lists

”’

Lists look like Tuples, but they have some critical differences. It’s very important to make out the differences

Tuples Lists

===================== ===================================

used () parenthesis Uses [] parenthesis

singlets need , (5,) No additional comma reqired [5]

Immutable, means elements can’t Important feature, Lists are mutable

be modified e.x.,: Trees=[‘Mango’, ‘Neem’, ‘Tamarind’]

we can replace Neem i.e. Trees[1]=’Banian’

Now Trees List will looks as

Trees=[‘Mango’, ‘Banian’, ‘Tamarind’]

”’

Trees=[‘Mango’, ‘Neem’, ‘Tamarind’]

Fruits=[‘Apple’, ‘Banana’]

print Trees, Fruits

#Modify the Trees lists

Trees[1]=’Banian’

print Trees

#using methods like append, we can extend Lists

Fruits.append(‘Mango’)

print Fruits

#Now we will see another example called “Aliasng”

Plants=[Trees,Fruits]

Plants1=[[‘Mango’, ‘Banian’, ‘Tamarind’], [‘Apple’, ‘Banana’, ‘Mango’]]

#if you print both look alike

#print Plants

#print Plants1

#Now modify Trees lists using append method

Trees[1]=’Neem’

print Plants

print Plants1

”’

OUTPUTs varies

Plants output [[‘Mango’, ‘Neem’, ‘Tamarind’], [‘Apple’, ‘Banana’, ‘Mango’]]

Plants1 output [[‘Mango’, ‘Banian’, ‘Tamarind’], [‘Apple’, ‘Banana’, ‘Mango’]]

”’

Advertisements

Author: rajukv

Hadoop(BigData) Architect and Hadoop Security Architect can design and build hadoop system to meet various data science projects.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s