- Updating list contents:
We can use the append() method or extend() method to add elements to a list. The append() method adds an object to the end of a list and extend() method adds each element of an iterable object to a list. To update an existing element in a list, assign the new value to that position using its index. See the examples below.
sample_list.append(1)
Output : [‘sample’, ‘list’, 45, ‘test’, 1]
sample_list.append([1, 2])
Output : [‘sample’, ‘list’, 45, ‘test’, [1, 2]]
sample_list.extend([1,3,2])
Output : [‘sample’, ‘list’, 45, ‘test’, 1, 3, 2]
sample_list[0] = ‘new_val’
Output : [‘new_val’, ‘list’, 45, ‘test’]
- To delete elements, we can use del statement.
del sample_list[2] : //deletes element with index 2.
- The sample_list.insert() method will insert an element at a particular position. The format is,
sample_list.insert(i, x). //It will insert element ‘x’ at position ‘i’.
- The method, sample_list.remove(x) will remove the first element in the list whose value is ‘x’.
sample_list.pop(i) removes the item at the given position in the list, and return it.
- If no index is specified, sample_list.pop() removes and returns the last item in the list.
- sample_list.clear() removes all the items from the list.
It is equivalent to del sample_list[:].
- sample_list.count(x) will return the number of times ‘x’ appears in the list.
- The reverse() method can be used to reverse the elements in a list
- copy() will create another copy of the list.
Tuples
Tuples are similar to lists. The main difference between list and a tuple is that tuples are immutable and their manipulation is faster than list.
A tuple is a collection of data separated by commas and enclosed in parentheses.
Example:
sample_tuple = (1,2,3)
We can create tuple without brackets also.
sample_tuple = 1,2,3
In case we need to create a tuple with a single element, we have to use a comma.
sample_tuple = (1, )
Like lists, we can access tuple elements using its index. There are two methods available with tuples.
- Index(), which will return an element’s occurrence in the list
my_tuple = ('a','p','p','l','e',)
my_tuple.index('l')
# output : 3
- ‘Count’ will find the number of occurrences of an element
my_tuple = ('a','p','p','l','e',)
my_tuple.count('p')
# output: 2
Dictionary
A dictionary contains a sequence of key-value pairs. We can access keys and values from a dictionary independently. Dictionary elements are enclosed by curly brackets.
d = {‘key1’: ‘first’, ‘key2’: [6, 7] }
- d.keys() will list all the keys and d.values() will list all the values of the dictionary.
In addition to keys and values methods, there is also
- items() method which returns a list of items in the form (key, value). The items are not returned in any particular order.
- In order to get the value corresponding to a specific key, use get or pop.
d.get(‘key1’) will return the value with key ‘key1’ if there is any. In above example, the output will be ‘first’
d.pop(‘key1’) will remove the pair with key ‘key1’ from the dictionary.
- popitem() removes and returns an arbitrary (key, value) pair from the dictionary;
- We can use del statement to remove a single element from a dictionary using its key
del d[‘key1’]
- clear() will remove all the values from a dictionary.
d.clear()
Sets
A set is an unordered collection with no duplicate elements. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference. We can use curly braces or the function set() to create sets.
Note: To create an empty set you have to use set(), not {}; the latter creates an empty dictionary.
basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(basket)