list - dictionary


list

In Python, a list is a collection of values stored in order so that you can access them by position. (In other programming language it may be called as "array"). You create a list using [ and ] to contain its initial contents:

a = [34, "Fred", 12, False, 72.3]
print(a[1])

it will show: Fred

You don’t need to specify the size of a list when you declare it, and you can also change the number of elements in the list any time you like. Use the [] notation to access elements of a list by their position in the list.  The list positions (indices) start at 0 for the first element.

As well as using the [] notation to read values out of a list, you can also use it to change values at a certain position:
a = [34, 'Fred', 12, False, 72.3]
a[1] = 777
print(a)
it will show: [34, 777, 12, False, 72.3]

If you try to change (or read) an element using an index that is too large, you will get an “Index out of range” error.

len(a)               <--display how many elements there are in the list
a.append("new")      <--add an element (in this case the word "new") to the end of the list
a.insert(2,"new2")   <--insert to a certain position ( 2: index, where it should be inserted, new2: the item to be inserted)
a.extend(b)          <--adds all the elements of a list: b to the end of list:a

a.pop()              <--removes the last element of the list (pop returns the value removed from the list)
a.pop(2)             <--removes the 3rd elemnt from the list (index starts from 0)
a.sort()             <--it puts in alphabetical order the elements of a list

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

dictionary

Lists are great when you need to access a list of items in order, or you always know the index of the element that you want to use. Dictionaries are an alternative to lists, but a dictionary stores key/value pairs in such a way that you can use the key to retrieve that value very efficiently and without having to search the whole dictionary.

To create a dictionary, you use the {} notation
phone_numbers = {'Simon':'01234 567899', 'Jane':'01234 666666'}

print(phone_numbers['Simon'])         <--it will show  the phone number of Simon
phone_numbers['Pete']='01234 777555'  <--adds a new entry to the dictionary
phone_numbers['Jane']='987 654 321'   <--if key is not in use new entry is added, if it is already used, it will overwrite
phone_numbers.pop('Jane')             <--remove the given item from the dictionary

Here the keys of the dictionary are strings, but they could be numbers or in fact any data type, although strings are most commonly used. The values can also be of any data type, including other dictionaries or lists.:

a = {'a_key1':'a_value1', 'a_key2':'a_value2'}
b = {'b_key1':a}

print(b) will show:
{'b_key1': {'a_key1': 'a_value1', 'a_key2': 'a_value2'}}

When you display the contents of a dictionary, you will notice that the order of the items in the dictionary may not match the order in which they were specified when the dictionary
was created. Unlike lists, dictionaries have no concept of keeping items in order.  The reason the order appears to be random is that the underlying data structure is a hash table. Hash tables use a hashing function to decide where to store the value; the hashing function calculates a numeric equivalent to any object.

---------------

for loop with dictionaries

Use the for command to iterate over the keys of the dictionary:

phone_numbers = {'Simon':'01234 567899', 'Jane':'01234 666666'}
for name in phone_numbers:
  print(name)
...
Jane
Simon

No comments:

Post a Comment