Day8

Pramod Ray
3 min readSep 12, 2019

--

List and Dictionary

A list is a data type that contains multiple values in an ordered sequence. In the the list value is called item or element . it is separated with comma and enclosed in [] square bracket.

Ex a=['ram','mohan',10,11,23,'india]

Operator in list

Operator Description :Repetition The repetition operator enables the list elements to be repeated multiple times.

L1*2 = [1, 2, 3, 4, 1, 2, 3, 4]

Concatenation : It concatenates the list mentioned on either side of the operator.

l1+l2 = [1, 2, 3, 4, 5, 6, 7, 8]

Membership :It returns true if a particular item exists in a particular list otherwise false.

print(2 in l1) prints True.

Iteration :The for loop is used to iterate over the list elements.

for i in l1: 
print(i)

Output

1
2
3
4

Length :It is used to get the length of the list

len(l1) = 4

Python List Built-in functions

Python provides the following built-in functions which can be used with the lists.

1 cmp(list1, list2) : It compares the elements of both the lists.

2 len(list): It is used to calculate the length of the list.

3 max(list): It returns the maximum element of the list.

4 min(list): It returns the minimum element of the list. 5 list(seq) It converts any sequence to the list.

Python List built-in methods

1 list.append(obj) :The element represented by the object obj is added to the list.

2 list.clear(): It removes all the elements from the list. 3 List.copy() It returns a shallow copy of the list.

4 list.count(obj) : It returns the number of occurrences of the specified object in the list.

5 list.extend(seq) : The sequence represented by the object seq is extended to the list.

6 list.index(obj) :It returns the lowest index in the list that object appears.

7 list.insert(index, obj) :The object is inserted into the list at the specified index.

8 list.pop(obj=list[-1]): It removes and returns the last object of the list.

9 list.remove(obj) :It removes the specified object from the list.

10 list.reverse() :It reverses the list. 11 list.sort([func]) It sorts the list by using the specified compare function if given.

Dictionary :dictionary is the collection of key-value pairs where the value can be any python object whereas the keys are the immutable python object.

Dictionary simulates Java hash-map in python.

Ex: dict=['Ram':100,'sohan':112,'Dravid'=270,'sehwag':319]

Built-in Dictionary functions

The built-in python dictionary methods along with the description are given below.

1 cmp(dict1, dict2): It compares the items of both the dictionary and returns true if the first dictionary values are greater than the second dictionary, otherwise it returns false.

2 len(dict) :It is used to calculate the length of the dictionary.

3 str(dict): It converts the dictionary into the printable string representation.

4 type(variable) :It is used to print the type of the passed variable.

Built-in Dictionary methods

The built-in python dictionary methods along with the description are given below.

1 dic.clear(): It is used to delete all the items of the dictionary.

2 dict.copy(): It returns a shallow copy of the dictionary.

3 dict.fromkeys(iterable, value = None, /) : Create a new dictionary from the iterable with the values equal to value.

4 dict.get(key, default = “None”) : It is used to get the value specified for the passed key.

5 dict.has_key(key): It returns true if the dictionary contains the specified key. 6 dict.items() It returns all the key-value pairs as a tuple.

7 dict.keys(): It returns all the keys of the dictionary.

8 dict.setdefault(key,default= “None”): It is used to set the key to the default value if the key is not specified in the dictionary

9 dict.update(dict2) :It updates the dictionary by adding the key-value pair of dict2 to this dictionary.

10 dict.values(): It returns all the values of the dictionary.

11 len(): To find the length of dictionary

12 popItem(): to pop element.

13 pop(): to pop element

14 count() : to count the value.

15 index(): To find index-value(position of elements)

16 append(): To update the value. insert new value.

--

--

Pramod Ray
Pramod Ray

No responses yet