Lists []

- 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

Overview

- 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

Creating Lists

- using using list (()) -> double round-brackets 
- using [] 

Creating Lists using list (())

In [40]:
my_list = list((10 , "hello" , [3,7], ("Room Number",3), "hello")) 

print(my_list)
[10, 'hello', [3, 7], ('Room Number', 3), 'hello']

Creating Lists using []

In [42]:
my_list = ['Hello']*5

your_list = [10 , "hello" , [3,7], ("Room Number",3), "hello"]

print(my_list)
print(your_list)
['Hello', 'Hello', 'Hello', 'Hello', 'Hello']
[10, 'hello', [3, 7], ('Room Number', 3), 'hello']

Index of Lists

- Index of last element of List is -1
In [7]:
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])
10
hello
[3, 7]
3
7
('Room Number', 3)
Room Number
3
('Room Number', 3)

Range of Indexes

- Showing multiple elements
In [9]:
my_list = list((10 , "hello" , [3,7], ("Room Number",3))) 

my_list[2:4]
Out[9]:
[[3, 7], ('Room Number', 3)]

Step Index

- step: the amount by which the index increases (defaults: 1)
In [43]:
my_list = [0,1,2,3,4,5,6,7,8,9,10]

my_list[::2]
Out[43]:
[0, 2, 4, 6, 8, 10]

Concatenating Lists (Combining Lists)

In [10]:
my_list = ( 2 , "Bye", 100)

new_list = my_list + (0.5 , "Hello", 150)

print(new_list)
(2, 'Bye', 100, 0.5, 'Hello', 150)

Extending Lists

- extend() takes more than one argument
- Adding new items to our list
In [11]:
my_list = [ 2 , "Bye", 100]

my_list.extend ([0.5 , "Hello", 150])

print(my_list)
[2, 'Bye', 100, 0.5, 'Hello', 150]

Appending to Lists

- 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

Appending to Lists - Adding a new string to a list

In [14]:
my_list = [ 2 , "bye", 100]

my_list.append ([0.5 , "Added", 150])

print(my_list)
[2, 'bye', 100, [0.5, 'Added', 150]]

Appending to Lists - Adding a new list to a list

In [15]:
my_list = [ 2 , "bye", 100]

my_list.append ("Hello")

print(my_list)
[2, 'bye', 100, 'Hello']

Appending to Lists - Adding a new (Integer/Float) to a list

In [17]:
my_list = [ 2 , "bye", 100]

my_list.append (4.5)

print(my_list)
[2, 'bye', 100, 4.5]

Insertig to Lists

- Inserting an element to the specified index
In [1]:
my_list = [ 2 , "bye", 100]

my_list.insert (0, "hello")

print(my_list)
['hello', 2, 'bye', 100]

Changing an Element of Lists using Index

In [19]:
my_list = [ 2 , "bye", 100]

my_list[0]="first element changed"

print(my_list)
['first element changed', 'bye', 100]

Reversing Lists

In [20]:
my_list = [ 2 , "bye", 100]

my_list.reverse()

print(my_list)
[100, 'bye', 2]

Sorting Lists

- list.sort()
- sorted(list)

Sorting Lists using list.sort()

In [29]:
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)
[3, 5, 12]
['a', 'k', 'z']

Sorting Lists using sorted(list)

In [38]:
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)
[3, 5, 12]
['a', 'k', 'z']

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)

Removing an element

In [30]:
my_list = [ 2 , "bye", 100]

del(my_list[0])

print(my_list)
['bye', 100]

Removing a range of elements

In [31]:
my_list = [ 2 , "bye", 100]

del(my_list[0:2])

print(my_list)
[100]

Removing an item with the specified index

In [32]:
my_list = [ 2 , "bye", 100]

my_list.pop(1)

print(my_list)
[2, 100]

Removing the last element

- If Index is not specified
In [33]:
my_list = [ 2 , "bye", 100]

my_list.pop()

print(my_list)
[2, 'bye']

Emptying Lists

In [35]:
my_list = [ 2 , "bye", 100]

my_list.clear()

print(my_list)
[]

Deleting lists

- 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
In [36]:
my_list = [ 2 , "bye", 100]

del my_list

print(my_list)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-36-3eba536ebd03> in <module>
      5 del my_list
      6 
----> 7 print(my_list)

NameError: name 'my_list' is not defined

Converting stirng to a list

In [2]:
my_sentence = "I am so excited"

my_sentence.split()
Out[2]:
['I', 'am', 'so', 'excited']

Aliasing

- If you change any item in my_list, your_list also changes
In [44]:
my_list = [ 2 , "bye", 100]

your_list = my_list

my_list[0] = 4

print(my_list)
print(your_list)
[4, 'bye', 100]
[4, 'bye', 100]

Copying Lists

- If you change any item in my_list, your_list will not change
- using [:] -> Cloning
- using copy()
- using list()
In [47]:
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)
[4, 'bye', 100]
[2, 'bye', 100]
[2, 'bye', 100]
[2, 'bye', 100]

Lenghth of Lists

In [98]:
my_list = [ 2 , "bye", 100]

len(my_list)
Out[98]:
3

Lists and Loop

In [3]:
my_list = [ 2 , "bye", 100]

for i in my_list:
   
    print(i)
2
bye
100

Creating a new List by Loop

In [50]:
my_list = [ 2 , 4, 6, 8, 10]

your_list = [i*i for i in my_list]
  
print(my_list)
print(your_list)
[2, 4, 6, 8, 10]
[4, 16, 36, 64, 100]

Lists and if Statements

In [101]:
my_list = [ 2 , "bye", 100]

if "bye" in my_list:
  print("Yes, You won!")

else:
    print("No, You lost")
Yes, You won!