Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

learning python part 3 (lists, dictionaries and tuples)

  • Please log in to reply
1 reply to this topic

#1 saeras


    CC Resident

  • Just Joined
  • PipPipPipPip
  • 64 posts

Posted 13 February 2010 - 12:26 PM

In this tutorial i will try my best to go over lists, dictionaries and tuple. All of these are really powerful and shouldn't be looked over as they provide really useful tools that will speed up the coding process considerably. Let's get started then.

The best way to describe a list i guess is to show you how to create one.

#this will create an empty list.
list = []

#if you want to add an item to this list you use .append
#like this.
list.append('something you want in the list')

#your list now contains one item
#so if you try to check its length using len()
#then len should return 1

#and if you want to check the first item in the list
#you do so in this manner

#or if you simply want the whole list returned
#you just type its name

#most of the times youll want to assign
#serval values to a list at once.
list = ['hi', 'to', 12, variable]
#notice that strings are contained in either ' or ". While integers
#aren't contained in anything. And if you want to insert an variable
#you simply insert its name together with the other items.

Also lists are mutable objects which mean you can edit it. This is mostly what differs tuples and lists. Meaning an tuple cant be edited. Or well it can be edited just not as easy and if you need to edit an tuple you should have used a list instead.
Well to make lists more clearly here are some examples.

#this example will go through items in the list
#and print them out
list = [1, 2, 3, 4, 5]

for x in list:

#this example will append the values 1, 2, 3, 4, 5
#into an list
list = []

for x in range(1, 5):

Following I will explain some commands that you can use on a list

#starting with a list with some numbers in it
list = [1,2,3]

#Say you want to extend it with another list
b = [4,5,6]

#using .append() would make b into an item in list.
#would make list return [1,2,3,[4,5,6]]
#meaning that b got nested into list

#instead you use .extend()
#this will make list return [1,2,3,4,5,6]

Using the same list all the time let's see the rest.
list.insert(i, x)
#Will insert x item into i index.

#Will remove the first occurrence of x if 
#there is no item x it will return a value error.

#will remove and return the item at 
#i index. If you skip writing out the i value 
#it will instead remove and return the last 
#item in the list.

#return the index of the first occurrence of x
#if x doesn't exist it will return an error

#return the number of times x appears in the list

List is quite the useful tool don't you think?

Next lets talk some about tuples. How i think about tuples are lists that are cut in stone. Meaning that once you describe a tuple you cannot change it.
You can change a tuple in the same manner you would use for example addition on integers.
a = (1, 2)
b = (3, 2)

a += b

This will create a new tuple also named a with the old a and b in it.

You can return a single item or the whole tuple in the same manner as you did with the list. That is using "[index]" or just typing it out in a whole.

#works in the same way as list.count(x)

#works in the same way as list.index(x)

Last but definitely not least we have dictionaries. I think I could spend a whole tutorial on elaborating the vast uses on dictionaries but I'm afraid i don't have all the time in the world so I will try and keep this somewhat short.

Dictionaries is pretty much what it sound like. As shown here

dict = {'Andreas':'something assigned to andreas'}
#now if i were to type
#it would return "'something assigned to andreas'"
#and if i would like to add another item to dict
#I could simply do this
dict['Another person'] = 'something assigned to another person'
#and if i would want to remove an item from dict
del dict['Another person']

Not so hard but definitely useful, say your creating an login system. Then you can store username and password together. and to get the password you would simply have to call the username.
The uses and commands of a dictionary doesn't end there but I will leave the rest to you or another tutorial.

This concludes part 3, I hope you have learned a lot about lists tuples and dictionaries and find them as useful as i do.

Feel anything have been left out? Had trouble with something? Or want something changed?
Post a comment about it and I will see what I can do.
  • 1
Posted Image

#2 Madhak


    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 04 March 2011 - 07:19 AM

Another good tutorial ... But when you say "you would simply have to call the username." Do you mean the
  • 0

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download