Inbuilt Data Structures in Python


In this article, we will learn about the 4 inbuilt data structures in python namely Lists, Dictionaries, Tuples & Sets.

Lists

A list is an ordered sequence of elements. It is a non-scalar data structure and mutable in nature. A list can contain distinct data types in contrast to arrays storing elements belonging to the same data types.

Lists can be accessed by the help of the index by enclosing index within square brackets.

Now let’s see an illustration to understand list better.

Example

lis=['tutorialspoint',786,34.56,2+3j]
# displaying element of list
for i in lis:
   print(i)
# adding an elements at end of list
lis.append('python')
#displaying the length of the list
print("length os list is:",len(lis))
# removing an element from the list
lis.pop()
print(lis)

Output

tutorialspoint
786
34.56
(2+3j)
length os list is: 5
['tutorialspoint', 786, 34.56, (2+3j)]

Tuples

It is also a non-scalar type defined in Python. Just like lists, it is also an ordered sequence of characters but tuples are immutable by nature. This means any modification is not allowed with this data structure.

Elements can be heterogeneous or homogenous nature separated by commas within the parenthesis.

Let’s take a look at an example.

Example 

tup=('tutorialspoint',786,34.56,2+3j)
# displaying element of list
for i in tup:
   print(i)

# adding elements at the end of the tuple will give an error
# tup.append('python')

# displaying the length of the list
print("length os tuple is:",len(tup))

# removing an element from the tup will give an error
# tup.pop()

Output

tutorialspoint
786
34.56
(2+3j)
length os tuple is: 4

Sets

It is an unordered collection of objects without any duplicates. This can be done by enclosing all the elements within curly braces. We can also form sets by using type casting via a keyword “set”.

Elements of a set must of immutable data types. Set doesn’t support indexing, slicing, concatenation & replication. We can iterate over elements using the index.

Now let’s take a look at an example.

Example

set_={'tutorial','point','python'}
for i in set_:
   print(i,end=" ")
# print the maximum and minimum
print(max(set_))
print(min(set_))
# print the length of set
print(len(set_))

Output

tutorial point python tutorial
point
3

Dictionary

A dictionary is an unordered sequence of key-value pairs. Indices can be of any immutable type and are called keys. This is also specified within curly braces.

We can access the values by the help of unique keys associated with them.

Let’s look at an example.

Example

# Create a new dictionary
d = dict()

# Add a key - value pairs to dictionary
d['tutorial'] = 786
d['point'] = 56

# print the min and max
print (min(d),max(d))
# print only the keys
print (d.keys())
# print only values
print (d.values())

Output

point tutorial
dict_keys(['tutorial', 'point'])
dict_values([786, 56])

Conclusion

In this article, we learned about the inbuilt data structures present in Python language and their implementation.

raja
Published on 07-Aug-2019 10:48:26
Advertisements