Learning Python: Lists, Tuples and Sets

Collection types

List

fruits_list = ["apple", "banana", "cherry"]

Tuple

fruits_tuple = ("apple", "banana", "cherry")

Set

fruits_set = {"apple", "banana", "cherry"}

Printing elements of lists, tuple and sets are similar

print("Print Fruits list")
for x in fruits_list:
    print(x)

print("")
print("Print Fruits tuple")
for x in fruits_tuple:
    print(x)

print("\n\nPrint Fruits Set")
for x in fruits_set:
    print(x)

But, they are on similar

print("But is list is equal to tuple {0}", \
    "yes" if fruits_list == fruits_tuple else "no")

print("\n\ngood lord.. why?")

print("\nlord: They are different :-)")

print("Tuples are ordered an unchangeable")

check if apple in a tuple?

if "apple" in fruits_tuple:
    print("lord: dumbo, yes I created the apple.")

Or check in a list

if "apple" in fruits_list:
    print("lord: you total dumbo, yes I created the apple.")

Accesing a list in negative order

print("Fruits list negative index: {0}".format(fruits_list[-1]))
print("Fruits tuple negative index: {0}".format(fruits_tuple[-1]))

Changing an item in list

fruits_list[0]  = "Apple-martini" # change the item value
fruits_list.append("Vodka-martini") #
print("Fruits and Martini are good for health: {0}".format(fruits_list))

Let’s dig into sets

print("Let's look at sets")

fruits_set = {"apple", "banana", "cherry"}
print("\n\nPrint Fruits Set 1")
for x in fruits_set:
    print(x)


fruits_set = {"apple-martini", "banana-gfy", "cherry-gfy"}
print("\n\nPrint Fruits Set 2")
for x in fruits_set:
    print(x)

print("If you observed, sets are unordered an unindexed!")
print("Once created you cannot change items, but you can add new items")

Lets look at dict .. they are important

sample_dict = {
    "oracle": "rdbms",
    "cassandra": "nosql",
    "redis": "KeyValueStore",
    "mssql": "rdbms"
}

**Print Dict

print("Dict of technologies: {0}".format(sample_dict))

Accessing item by key name


print("Oracle is {0} technology".format(sample_dict["oracle"]))
print("And MsSQL is {0} technology".format(sample_dict.get("mssql")))

Changing dict

sample_dict["oracle"] = "sql & nosql"
print("Oracle is {0} technology".format(sample_dict["oracle"]))

and, we can loop through a dict

print("\n\nPrint dict")
for x in sample_dict:
    print(x)

we can also get values()

print("\n\nPrint dict values")
for x in sample_dict.values():
    print(x)

we can print name and value

print("\n\nPrint Key Value")
for x, y in sample_dict.items():
    print("{0}={1}".format(x, y))

check if key exists

if "oracle" in sample_dict:
    print("Yes, there is ORACLE. .... tsk tsk ... ASK NEO")

remove an item

sample_dict.pop("oracle")
if "oracle" not in sample_dict:
    print("NO, there is no ORACLE ... tsk tsk.. ASK DORTHARAKI, oracle got popped")

little bit of memory now..

sample_dict_pointer = sample_dict
print("Is sample_dict_pointer = sample_dict? {0}".format(sample_dict == sample_dict_pointer))
sample_dict_copy = sample_dict.copy()
print("Is sample_dict_copy = sample_dict? {0}".format(sample_dict == sample_dict_copy))

Ok they are same.. than what is the difference

print("\nAFTER ADDING BACK ORACLE")
sample_dict["oracle"] = "i made it to the dortharaki lands"
print("Is sample_dict_copy = sample_dict? {0}".format(sample_dict == sample_dict_copy))
print("Is sample_dict_pointer = sample_dict? {0}".format(sample_dict == sample_dict_pointer))
result = "ORACLE IN SAMPLE_DICT (Original)" if "oracle" in sample_dict \
    else "ORACLE not in SAMPLE DICT (ORIGINAL)"
print(result)
result = "ORACLE IN SAMPLE_DICT_POINTER" if "oracle" in sample_dict_pointer \
    else "ORACLE not in SAMPLE_DICT_POINTER"
print(result)
result = "ORACLE IN sample_dict_copy" if "oracle" in sample_dict_copy \
    else "ORACLE not in sample_dict_copy"
print(result)

You notice “ORACLE is not in sample_dict_copy”