PYTHON LISTS AND DICTIONARIES

From the previous text we talked about numbers and string in python. From this text I’m going to talk about Lists and Dictionaries in Python. Both are valuable data types in python. Where we can use lists and dictionaries to store and manipulate data. There are built in functions and methods associated with them as well. Let’s see some of them with examples.

LISTS

  • We can simply create a List by giving ‘[]‘ command with the data to be stored in the relevant list.

1

  • Then after creating a List in python we can access data stored in it from the index.

2

  • Also we can use slicing with Lists as well.

3

  • Also ‘+‘ and ‘*‘ uses in Lists as same as how we use them with strings. + to concatenate and * to repeat.

4

  • Using the ‘append()‘ method we can add data items at the end of the list.

5

  • pop()‘ is used to remove data items, when it removes it will display the value as well.

6

  • Using del function we can remove the reference to that particular list as we talked in strings.

7

  • insert()‘ is used to insert data in to a particular index at the relevant list.

8

9

  • remove()‘ can be used to remove a item from the list from referencing it by its value.

10

  • sort()‘ will the sort the entire list.

13.png

  • If we want to reverse the list, then we can use the ‘reverse()‘ method.

14

  • Also we can create matrices using the lists in python. This is something like the two dimensional arrays used in JAVA.

16

17

  • Using the ‘range()‘, for creating a range of values, with ‘list()‘ will enable us to create a list in a particular range.

19

  • Using the ‘sum()‘ we can calculate the total sum of a list containing numbers.

20

  • To get the maximum value in a list, we can use the ‘max()‘ method and to get the lowest value, we can use the ‘min()‘ as well.

21

Those are some methods and functions used with lists in python. Next we will see how to use Dictionary in python.

Dictionary

  • Dictionary has a ‘key:value‘ pair separated with ‘,‘. Using the name of the dictionary we can access the values as well.

23

  • As we did with the lists, using the key, we can get a particular value only.

24

  • Also we can use mathematical operators with the values stored in the dictionaries.

25.png

  • dict()‘ is another method used in python to create dictionaries.

28.png

  • We can use ‘in‘ to find out the membership of a key in a dictionary. If the key is there in the dictionary it will output ‘True‘ and if it’s not it will output ‘False‘.

29

  • If we are not sure about a key in the dictionary, we can use it with the ‘get()‘ method. Then we can give a default value to print, if the key is not available.

30

These are some methods used with dictionaries in Python. Hope you got an idea about these two types. Hope to see you soon with another Python blog post. Thank You!

Advertisements

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