Python-Lists, Tuples and Sets

Source: Deep Learning on Medium


Go to the profile of Develop It

#Lists []
#Tuples ()
#Sets {}

#Empty List
#List = []
#List = list()

#Empty Tuples
#Tuple = ()
#Tuple = tuple()

#Empty Sets
#Set = {} #Using {} is not the right way because {} is for dict.
#Set = set()

#Empty Dictionary
#Dict = {}

#Lists
Elements = [‘A’, ‘B’, ‘C’]
Numbers = [‘0’, ‘1’, ‘2’]
Strings = [‘Hi’, ‘Bye’]

print(Elements[0])
print(Elements[-1])
print(Elements[:2])

Elements.append(‘D’) #Append adds 1 thing whereas extend adds multiple things. If you add multiple things with append then it will the list directly to another list which look different then what you get with extend.
Elements.remove(‘C’)
popped = Elements.pop() #If you want to use the list like a stack or a queue.
print(popped) #It returns the value that it removes
print(Elements)

Elements.insert(2, ‘C’) #Insert at index 2 and it doesn’t overwrite.
print(Elements)

Elements.insert(2, Numbers) #This creates a list inside of another list.
print(Elements)
print(Elements[2]) #The output of this is the Numbers list

#If you want to add multiple values to your list then use extend.
Strings.extend(Numbers)
print(Strings)

Strings.reverse() #Just Reverses the list.
print(Strings)

Strings.sort() #Sorts the List in alpha-numeric order. Ascending and Alphabetical Sort.
print(Strings)

Strings.sort(reverse = True) #Descending order
print(Strings)
#We don’t need to reset the list before calling most of these methods because they are just altering the list.

Strings_sorted = sorted(Strings) #By doing this our original list doesn’t get changed.
print(Strings_sorted)
print(Strings)

print(min(Strings))
print(max(Strings))

num = [1, 2, 3]
print(‘sum(num) = {}’ .format(sum(num)))

print(‘index(Hi) = {}’ .format(Strings_sorted.index(‘Hi’))) #This prints the index of Hi in Strings_sorted.

print(‘1 in num = {}’ .format(1 in num)) #You can make use of the ‘in’ operator to know if something is present in the list or not.

s = “shivam”
#print(f’{s})

for item in num: #After each loop the item gets assigned to the next item in the next.
 print(item) #By default it prints on a new line everytime its executed.
#item is not a keyword and we can name it anything.

for index, item in enumerate(num, start = 1): #With enumerate we can get the index and we can get a better output.
 print(index, item) #By using start we can also change the index starting value.

Strings_str = ‘, ‘.join(Strings)
print(Strings_str)

Join_with_Hyphen = ‘ — ‘.join(Strings)
print(Join_with_Hyphen)

Split_String = Join_with_Hyphen.split(‘ — ‘)
print(Split_String)

# — — — — — — — — — — — — — — — — — — — — — —

#Tuples

#Tuples are very similar to Lists except the case that they are immutable as they cannot be modified.
#In Tuples we use parenthesis and not square brackets.

list_1 = (‘A’, ‘B’, ‘C’)
list_2 = list_1

print(list_1)
print(list_2)

#list_1[0] = ‘D’ #Tuples are immutable and they cannot be modified.
####If you run the above statement then it will give you an error as tuples are immutable.
#print(list_1)
#print(list_2)
#List_1 changes List_2

tuple_1 = (‘A’, ‘B’, ‘C’)
tuple_2 = tuple_1

print(tuple_1)
print(tuple_2)

#tuple_1[0] = ‘D’ #Tuples are immutable and they cannot be modified.
#If you run the above statement then it will give you an error as tuples are immutable.
#print(tuple_1)
#print(tuple_2)

#Use a tuple if you don’t want to Modify.
#If you want to modify then use Lists.

# — — — — — — — — — — — — — — — — — — — — —

#Sets

####Sets are Values that are Unordered and also have No Duplicates.
#For Sets we use {}

set_1 = {‘A’, ‘B’, ‘C’}
print(set_1) #The order of the output is unordered and it goes on changing if you print out again and again.

####Sets are used to test whether a value is part of a set.
####Sets are used a lot to remove duplicate values because they throw away Duplicates.

set_2 = {‘A’, ‘B’, ‘C’, ‘C’} #The Set will throw away duplicates.
print(set_2)

#Membership Test is the test in which we see whether a value is part of a set.
#Sets do this a lot more efficiently than Lists and Tuples.
print(‘A’ in set_1)

####Sets Quickly determine what values they either share or don’t share with other sets.
set_3 = {‘A’, ‘D’, ‘E’}

print(set_1.intersection(set_3))
print(set_1.union(set_3))
print(set_1.difference(set_3))

# — — — — — — — — — — — — — — — — — — — — — — — — — —