basic-data-structures-in-python.png
Blogger_636313952775783385.jpg
By: Linto

Basic Data structures in python

Python is a widely used high-level programming language for general-purpose programming. Python is a simple, powerful and easy to learn the programming language. It is commonly used for Web and Internet development, Scientific and Numeric computing, Business application and Desktop GUI development etc. The basic data structures in python are lists, dictionaries, tuples, strings and sets

In this blog, we will get to know the five basic data structures of Python namely 
 - Lists 
 - Dictionaries
 - Tuples
 - Strings 
 - Sets

List 
A list is a collection of data which are separated by commas. The contents of a list are enclosed by square brackets. It can contain elements of different types. 
Example:
		sample_list = [‘sample’, ‘list’, 45, ‘test’]

 - Accessing list contents:
Accessing an element of a list is simple. We can access each element in a list using its index. The index starts from zero. 
Example:
		print sample_list[0]  - 
Output: sample
Print sample_list[1:3] -
Output : [‘list’, 45]
- 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.
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)  





Comments

0


Leave a comment