Ever wondered "What is Data Structures?" and "Why is Datastructures", This blog will help you with those questions in Python.
Data structures are a way of organizing and storing data so that they can be accessed and worked with efficiently.Lets Dicuss about the Different Data Types in Python.
Different Built-in Data Structures of Python:
- List
- Dictionary
- Tuple
- Sets
Method | Description |
---|---|
append() | Adds an element at the end of the list |
clear() | Removes all the elements from the list |
copy() | Returns a copy of the list |
count() | Returns the number of elements with the specified value |
extend() | Add the elements of a list (or any iterable), to the end of the current list |
index() | Returns the index of the first element with the specified value |
insert() | Adds an element at the specified position |
pop() | Removes the element at the specified position |
remove() | Removes the first item with the specified value |
reverse() | Reverses the order of the list |
sort() | Sorts the list |
Note: Python does not have built-in support for Arrays, but Python Lists can be used instead.
.append()
appends an object to a list:
>>> a = ['a', 'b']
>>> a
['a', 'b']
>>> a.append(123)
>>> a
['a', 'b', 123]
>>> a = ['a', 'b']
>>> a
['a', 'b']
>>> x = a.append(123)
>>> x
>>> print(x)
None
>>> type(x)
>class 'NoneType'>
>>> a
['a', 'b', 123]
>>> a = ['a', 'b']
>>> a + [1, 2, 3]
['a', 'b', 1, 2, 3]
>>> a
['a', 'b']
>>> a.append([1, 2, 3])
>>> a
['a', 'b', [1, 2, 3]]
>>> a = ['a', 'b']
>>> a
['a', 'b']
>>> a.append('hello')
>>> a
['a', 'b', 'hello']
.extend()
extends a list by appending elements from the iterable:
>>> a = ['a', 'b']
>>> a
['a', 'b']
>>> a.extend([1, 2, 3])
>>> a
['a', 'b', 1, 2, 3]
>>> a = ['a', 'b']
>>> a
['a', 'b']
>>> a += [1, 2, 3]
>>> a
['a', 'b', 1, 2, 3]
.insert(<index>, <obj>)
inserts the object <obj>
into the list at the specified <index>
, pushing the remaining list elements to the right:
>>> a = ['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a.insert(3, 3.14159)
>>> a[3]
3.14159
>>> a
['spam', 'egg', 'bacon', 3.14159, 'tomato', 'ham', 'lobster']
.remove(<obj>)
removes the first occurence of the value <obj>
and raises a ValueError
exception if the value is not present:
>>> a = ['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a.remove('egg')
>>> a
['spam', 'bacon', 'tomato', 'ham', 'lobster']
>>> a.remove('egg')
Traceback (most recent call last):
File "<input>", line 1, in <module>
a.remove('egg')
ValueError: list.remove(x): x not in list
.clear()
removes all items from the list:
>>> a = ['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a.clear
>>> a
[]
.sort(<key=None>, <reverse=False>)
sorts the list items in ascending order. An optional function can be used as a key
Here’s an example:
>>> a = ['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a.sort()
>>> a
['bacon', 'egg', 'ham', 'lobster', 'spam', 'tomato']
>>> a += ['Apple', 'Zebra']
>>> a
['bacon', 'egg', 'ham', 'lobster', 'spam', 'tomato', 'Apple', 'Zebra']
>>> a.sort()
>>> a
['Apple', 'Zebra', 'bacon', 'egg', 'ham', 'lobster', 'spam', 'tomato']
>>> a.sort(key=str.upper)
>>> a
['Apple', 'bacon', 'egg', 'ham', 'lobster', 'spam', 'tomato', 'Zebra']
>>> a.sort(key=str.upper, reverse=True)
>>> a
['Zebra', 'tomato', 'spam', 'lobster', 'ham', 'egg', 'bacon', 'Apple']
>>> b = [1, 77, 98, 34]
>>> b
[1, 77, 98, 34]
>>> b.sort()
>>> b
[1, 34, 77, 98]
>>> b += ['apple']
>>> b
[1, 34, 77, 98, 'apple']
>>> b.sort()
Traceback (most recent call last):
File "<input>", line 1, in <module>
b.sort()
TypeError: '<' not supported between instances of 'str' and 'int'
.reverse()
reverses the list in place:
>>> a = ['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a.reverse()
>>> a
['lobster', 'ham', 'tomato', 'bacon', 'egg', 'spam']
>>> a.reverse()
>>> a
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a[::-1]
['lobster', 'ham', 'tomato', 'bacon', 'egg', 'spam']
>>> a
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a = a[::-1]
>>> a
['lobster', 'ham', 'tomato', 'bacon', 'egg', 'spam']
Initialize an empty Dictionary
d = {}
Add an element with key “name” to the Dictionary
d["name"]
= 'ram'{'name':'ram'}
Update the element with key “name”
d["name"] = 'tom'
d['age'] = 25{'name':'tom','age':25}
Get element with key “k”
d["name"]'tom'
-- If the key is not present, a KeyError is raised
Get the list of keys
d.keys()
['name','age']
Get the size of the dictionary
len(d)
2
Comments
Post a Comment