In Python, there is primarily two data types mainly **Atomic **and Collective. Atomic data types are indivisible which represents only a single data value. Some examples are integer (int), floating point (float) and boolean (bool). **Collective **data types on the other hand are a collections of multiple data values which consists of string (str), list (list), tuple (tuple), set (set) and dictionary (dict). This article will focus on list, **set **and dictionary, and ways to convert between them without any inbuilt python commands.

List

Python list is a data type for a collection items which are generally related and they can hold data objects of any data type (i.e. string, int, bool) as shown below. They are usually captured using square brackets (i.e. [ ])

a_list = [1, 'two', 3.0, '4']

Some common useful list commands are as follow. Note that indexing in Python starts at 0 instead of 1 in other programming languages like R.

## define empty list
a_list = []

## append new item into list at the end of the list
a_list.append(new_item)
## append new item into list at a specific index
a_list.insert(index, new_item)
## remove an item from the list based on index
a_list.pop(0)
## remove an item from the list based on item value
a_list.remove('two')
## sort the items in ascending order, note that the data types must be similar
a_list.sort()
## reverse the order of the items
a_list.reverse()

#getting-started #data-science #python #data-analysis #data-structures

Python Data Structures Conversions (List , Set & Dictionary)
1.40 GEEK