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.
- We can simply create a List by giving ‘‘ command with the data to be stored in the relevant list.
- Then after creating a List in python we can access data stored in it from the index.
- Also we can use slicing with Lists as well.
- Also ‘+‘ and ‘*‘ uses in Lists as same as how we use them with strings. + to concatenate and * to repeat.
- Using the ‘append()‘ method we can add data items at the end of the list.
- ‘pop()‘ is used to remove data items, when it removes it will display the value as well.
- Using del function we can remove the reference to that particular list as we talked in strings.
- ‘insert()‘ is used to insert data in to a particular index at the relevant list.
- ‘remove()‘ can be used to remove a item from the list from referencing it by its value.
- ‘sort()‘ will the sort the entire list.
- If we want to reverse the list, then we can use the ‘reverse()‘ method.
- Also we can create matrices using the lists in python. This is something like the two dimensional arrays used in JAVA.
- Using the ‘range()‘, for creating a range of values, with ‘list()‘ will enable us to create a list in a particular range.
- Using the ‘sum()‘ we can calculate the total sum of a list containing numbers.
- 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.
Those are some methods and functions used with lists in python. Next we will see how to use Dictionary in python.
- Dictionary has a ‘key:value‘ pair separated with ‘,‘. Using the name of the dictionary we can access the values as well.
- As we did with the lists, using the key, we can get a particular value only.
- Also we can use mathematical operators with the values stored in the dictionaries.
- ‘dict()‘ is another method used in python to create dictionaries.
- 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‘.
- 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.
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!