- Lists = [x, y, (m,n), [p,q]]
- Lists are ordered Collections or ordered Sequences
- Lists are mutable -> List can be changed
- Lists allows duplicate elements
- Lists can contain string, integer, float, tuples and even lists (called nesting)
- Index of first element of Lists (x) is 0
- Index of last element of Lists ([p,q]) is -1
- Creating Lists
- using using list (()) -> double round-brackets
- using []
- Index of Lists
- Range of Indexes
- Step Index
- Concatenating Lists (Combining Lists)
- Extending Lists
- Appending to Lists
- Adding a new string to a list
- Adding a new list to a list
- Adding a new (Integer/Float) to a list
- Insertig to Lists
- Changing an Element of Lists using Index
- Reversing Lists
- Sorting Lists
- using list.sort()
- using sorted(list)
- Removing Elements from Lists
- Removing an element(del)
- Removing a range of elements(del)
- Removing an element with the specified index(pop)
- Removing the last element(pop)
- Emptying Lists
- Deleting lists
- Converting stirng to a list
- Aliasing
- Copying Lists
- using [:] -> Cloning
- using copy()
- using list()
- Lenghth of Lists
- Lists and Loop
- Creating a new List by Loop
- Lists and if Statements
- using using list (()) -> double round-brackets
- using []
my_list = list((10 , "hello" , [3,7], ("Room Number",3), "hello"))
print(my_list)
my_list = ['Hello']*5
your_list = [10 , "hello" , [3,7], ("Room Number",3), "hello"]
print(my_list)
print(your_list)
- Index of last element of List is -1
my_list = list((10 , "hello" , [3,7], ("Room Number",3)))
print(my_list[0])
print(my_list[1])
print(my_list[2])
print(my_list[2][0])
print(my_list[2][1])
print(my_list[3])
print(my_list[3][0])
print(my_list[3][1])
print(my_list[-1])
- Showing multiple elements
my_list = list((10 , "hello" , [3,7], ("Room Number",3)))
my_list[2:4]
- step: the amount by which the index increases (defaults: 1)
my_list = [0,1,2,3,4,5,6,7,8,9,10]
my_list[::2]
my_list = ( 2 , "Bye", 100)
new_list = my_list + (0.5 , "Hello", 150)
print(new_list)
- extend() takes more than one argument
- Adding new items to our list
my_list = [ 2 , "Bye", 100]
my_list.extend ([0.5 , "Hello", 150])
print(my_list)
- append() takes exactly one argument
- Adding a new string to a list
- Adding a new list to a list
- Adding a new (Integer/Float) to a list
my_list = [ 2 , "bye", 100]
my_list.append ([0.5 , "Added", 150])
print(my_list)
my_list = [ 2 , "bye", 100]
my_list.append ("Hello")
print(my_list)
my_list = [ 2 , "bye", 100]
my_list.append (4.5)
print(my_list)
- Inserting an element to the specified index
my_list = [ 2 , "bye", 100]
my_list.insert (0, "hello")
print(my_list)
my_list = [ 2 , "bye", 100]
my_list[0]="first element changed"
print(my_list)
my_list = [ 2 , "bye", 100]
my_list.reverse()
print(my_list)
- list.sort()
- sorted(list)
my_list_numbers = [ 12 , 5 , 3]
my_list_numbers.sort()
my_list_alphabets = [ "z" , "k" , "a"]
my_list_alphabets.sort()
print(my_list_numbers)
print(my_list_alphabets)
my_list_numbers = [ 12 , 5 , 3]
sorted_list_numbers = sorted(my_list_numbers)
my_list_alphabets = [ "z" , "k" , "a"]
sorted_list_alphabets = sorted(my_list_alphabets)
print(sorted_list_numbers)
print(sorted_list_alphabets)
- Removing an element(del)
- Removing a range of elements(del)
- Removing an element with the specified index(pop)
- Removing the last element(pop)
my_list = [ 2 , "bye", 100]
del(my_list[0])
print(my_list)
my_list = [ 2 , "bye", 100]
del(my_list[0:2])
print(my_list)
my_list = [ 2 , "bye", 100]
my_list.pop(1)
print(my_list)
- If Index is not specified
my_list = [ 2 , "bye", 100]
my_list.pop()
print(my_list)
my_list = [ 2 , "bye", 100]
my_list.clear()
print(my_list)
- When you delete the list -> your list no longer exist
- If you call your list -> it causes an error
- NameError: name 'my_list' is not defined
my_list = [ 2 , "bye", 100]
del my_list
print(my_list)
my_sentence = "I am so excited"
my_sentence.split()
- If you change any item in my_list, your_list also changes
my_list = [ 2 , "bye", 100]
your_list = my_list
my_list[0] = 4
print(my_list)
print(your_list)
- If you change any item in my_list, your_list will not change
- using [:] -> Cloning
- using copy()
- using list()
my_list = [ 2 , "bye", 100]
your_list_1 = my_list[:]
your_list_2 = my_list.copy()
your_list_3 = list(my_list)
my_list[0] = 4
print(my_list)
print(your_list_1)
print(your_list_2)
print(your_list_3)
my_list = [ 2 , "bye", 100]
len(my_list)
my_list = [ 2 , "bye", 100]
for i in my_list:
print(i)
my_list = [ 2 , 4, 6, 8, 10]
your_list = [i*i for i in my_list]
print(my_list)
print(your_list)
my_list = [ 2 , "bye", 100]
if "bye" in my_list:
print("Yes, You won!")
else:
print("No, You lost")