forked from kal179/Beginners-Python-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listOperationsMethods.py
49 lines (40 loc) · 1.63 KB
/
listOperationsMethods.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# list operations part 2
siliconValley = ['Google','Apple','Dropbox','Facebook','Cisco','Adobe','Oracle','Samsung']
print(siliconValley)
# hmm seems like i forgot to add Electronic Arts in the list siliconValley
# This will add the element at the end of the list
siliconValley.append('Electronic Arts')
print(siliconValley)
# thats cool but I want my element at specific position
siliconValley.insert(5, 'AMD')
# 5 is the position and whatever you add after comma is element
# Okay enough I want to pop out an element from list and I want to use it in a string
# you have to provide the index of elementyou want to pop out
poppedElement = siliconValley.pop(4)
print('Popped element is ' + poppedElement)
# Oops I Samsung isnt in silicon valley, I have to remove Samsung from list
# How am I gonna do thats
# You have to enter the element in parenthesis and not it's index
siliconValley.remove('Samsung')
print(siliconValley)
# I want to sort the list in alphabetical order
# How to do thats
# simple
siliconValley.sort()
# or
sorted(siliconValley)
print(siliconValley)
# I wanted list in reverse alphabetical order
# simple
siliconValley.sort(reverse = True)
# or
sorted(siliconValley , reverse = True) # seperate the reverse with comma
print(siliconValley)
# Okay what if i dont know about the index of an element but i want to print only that element
googleIndex = siliconValley.index('Google')
print(siliconValley[googleIndex])
# I am tired of watching those elements again and again
# How I am going to do thats
# easy
del siliconValley
print(siliconValley) # this should probably give you an NameError