[how] Organize Data Using List, Tuple, Set and Dictionary

  





List, Tuple, Set, and Dictionary are the commonly used Python data structures for storing and organizing collections of data. They are useful for processing tabular data which are typical in database and artificial intelligence fields.

The choice of their selections could be based on their four distinguished features:
  1. Mutable - The values in List and Dictionary are mutable (can be changed after they have been declared)  while the values in Tuple and Set are Immutable (cannot be changed, unless the Tuple and Set are redeclared).
  2. Ordered - The data in List and Tuple are ordered and thus they can be sorted.
  3. Unique - Set/Dictionary consists of unique Values/Keys.
  4. Indexed - List and Tuple contains Index value and therefore their data elements are searchable by index position. Dictionary contains unique Keys which also make their data elements searchable. 

LIST

# various ways of declaring a list

list1=[1,"abc",2.5]
print(type(list1))
print(list1)

list2=[]  # creates an empty list
print(type(list2))
print(list2)

list3=list((1,"abc",2.5)) #casting from tuple
print(type(list3))
print(list3)



TUPLE

# various ways of declaring a tuple

tuple1=(1,"abc",2.5)
print(type(tuple1))
print(tuple1)

tuple2=() # creates an empty tuple
print(type(tuple2))
print(tuple2)

tuple3=tuple([1,"abc",2.5]) #casting from list
print(type(tuple3))
print(tuple3)



SET

# various ways of declaring a set

set1={1,"abc",2.5}
print(type(set1))
print(set1)

set2=set() #creates empty set
print(type(set2))
print(set2)

set3=set([1,"abc",2.5,1,"abc"]) #casting from list
print(type(set3))
print(set3)

set4=set((1,"abc",2.5,1,"abc")) #casting from tuple
print(type(set4))
print(set4)



DICTIONARY

# various ways of declaring a dictionary

dict1={"key1":"1","key2":"abc","key3":"2.5"}
print(type(dict1))
print(dict1)

dict2={}   # empty dictionary
print(type(dict2))
print(dict2)

dict3=dict(zip(["key1","key2","key3"],[1,"abc",2.5]))
print(type(dict3))
print(dict3)



Post a Comment

1 Comments

  1. Sometimes list data are kept in dataframe colums in "stringified" form.
    We need to destringified first, in two ways:
    (1) strg_tk=" ".join([(text.replace("[","").replace(",","").replace("'","").replace("]","")) for text in dftk['token3']])
    or
    (2) strg_tk=" ".join([(re.sub(r"[\[\]\'\,]","",sentence)) for sentence in dftk['token3']])

    ReplyDelete