Udacity CS 101: Lists Part II (append, +, len)

There is a specific method called append() provided by lists. It is used to append an element to a list. This mutates the original list (it does not create a new list.:

listSay = ['How','Are']listSay.append('You')

print listSay # will give you ['How','Are','You']

This might remind you of the ‘+’ operator we use for joining strings, lists can use this operator too:

listSay = ['How','Are']

listGreet = ['You','Today']

hello = listSay + listGreet

print hello # will give you ['How','Are','You','Today']

# this however creates a new list and assigns it to the variable hello

Another useful procedure that we can use on lists is len(), this counts the number of elements in a list.

listSay = ['How','Are']

print len(listSay) # will give you 2

listGreet = ['How',['Are','You','Today']]

print len(listGreet) # will give you 2 too

As you can see, len() will not probe into nested list, it just gives a head-count of the number of first-level elements in the list.

We can also use len() on strings, which will give us the number of characters in the string:

greet = 'hello!'

print len(greet) # will give us 

The subsequent video lectures talk about CPU register, DRAM, hard drive and memory in general. I haven’t got much to cover on that so we’ll skip that, and I’ll see you at “For Loops”.

This Udacity post references Udacity CS 101 Unit 3 Chapters 14, 15

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