## Monday, March 20, 2017

### List

Creating an Empty list:
emptyList = []

Here is an example how to create a list with few values:
teamList = ['Real Madrid', 'FC Barcelona', 'Arsenal', 'LA Galaxy']
teamList.insert(2,"Bayern Munich")
Output:
Can we sort it backward?
teamList.sort(reverse =True)
Output:

Print a series of Numbers:
numbers = list(range(1,10))
Finding an element position:
print(peopleList[indexOfDario])
lastPerson = peopleList [len(peopleList)-1]
Extending one list to another list

isList = print(isinstance(name,list))
print (numbers [::2])

After a list is created, you can access a particular element by:
print(teamList[0])
note: Index position starts at 0

To access last element in the list we can use:
print(teamList[-1])

You can also concatenate messages with your list selection:
print('The best soccer club in the world is: " + teamList[0])

Output: The best soccer club in the world is Real Madrid

You can also concatenate a message with each element of the list:
for i in teamList:
print("My favorite teams are: " + i)

Output:
My favorite teams are: Real Madrid
My favorite teams are: FC Barcelona
My favorite teams are: Arsenal
My favorite teams are: LA Galaxy

Modifying elements in a list
List can be modify once they are created:
teamList[1] = 'Chelsea'

Now instead of having in index (1): Barcelona our value for the index will be Chelsea.

Adding Elements to the list. This is our current list
teamList = ['Real Madrid', 'Chelsea', 'Arsenal', 'LA Galaxy']

teamList.append("FC Barcelona")
print (teamList)

Output:
Real Madrid, Chelsea, Arsenal, LA Galaxy, FC Barcelona

Inserting an element at any position in the list.
print(teamList)

Output:
Real Madrid, Chelsea, Bayern Munich, Arsenal, LA Galaxy, FC Barcelona

Removing Elements from a list:

del teamList[5]

Output:
Real Madrid, Chelsea, Bayern Munich, Arsenal, LA Galaxy
As you can see FC Barcelona is not longer in the list.

del vs pop()
The pop() method allows you to remove the last item in the list but it lets you work with that item after removing it.
exFavoriteTeam = teamList.pop()
print(teamList)

Output:
Real Madrid, Chelsea, Bayern Munich, Arsenal
As you can see LA Galaxy is gone but if type print(exFavoriteTeam), Voila!

Ouput:
LA Galaxy

note: You can use pop() to delete any element of the list, same way as you delete them.

Items can be not only remove by position but they could also be remove by value:
teamList.remove('Arsenal')

Output:

List can be sorted:
teamList.sort()
Finding the length of our list:
len(teamList)

Output:
3

print(numbers)

Output:
1,2,3,4,5,6,7,8,9

min(numbers)
>>1

max(numbers)
>>9

sum(numbers)
>>45

indexOfDario =peopleList.index("Dario")
print(indexOfDario)

print(peopleList[0])

print("Last person is "+ lastPerson)

weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
weekend = ["Saturday", "Sunday"]
weekdays.extend(weekend)
print(weekdays)

Output:
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday

print (name)
#equivalent
print(emptyList[2][0])

Slicing a list use [start:stop:step]. Start os Inclusive, stop is Exclusive:
weekdays =  ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
lastTwoDays = weekdays [2:4]
print (lastTwoDays)

Output:
Wednesday, Thursday

numbers = [0,1,2,3,4,5,6,7,8,9]
print (numbers [:]) # begining to the end
print (numbers [2:7])

print (numbers[::-1])#reverse

Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 3, 4, 5, 6]
[0, 2, 4, 6, 8]

[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]