[Python] How to make lists ???????

HOW to make Lists in Python ?

It is actually easy to make lists in Python, Let's Start

patients = ["John", "Donald", "Kevin"]

when you make the lists, you're going to use square brackets [ ] for the values of the Lists.

Let's say, we'd like to print "John" on the screen, what would you do? 

Like I mentioned earlier on the other chapter, In Python index number starts from Zero. 

       ["John", "Donald", "Kevin"]
Index :   0         1        2

so John is 0, Donald is 1, Kevin is 2

Now we'd like to take out John to the screen, what are you going to do? 

right,  print(patients[0])

Then we get "John"

Let's make more values patients = ["John", "Donald", "Kevin", "Tony", "Alice"]

print(patients[1:])

1: means you're going to bring from index 1 to the end. 

So you get "Donald", "Kevin", "Tony", "Alice"

But when you run print(patients[1:3])
you're going to get from 1 to 2

So "Donald", "Kevin"

-----------------------------------------------------------------------------------

Question) print(patients[:3])

What are you going to get ? 






right,  ["John", "Donald", "Kevin"]
--------------------------------------------------------------------------------------

Modification

you can also modify some values like this 
patients = ["John", "Donald", "Kevin", "Tony", "Alice"]

patients[1] = "Nancy"

so if you want to change Donald into Nancy, you can say its index to the name you want patients[1] = "Nancy"

Then when you print print(patients[1])

You're going to get "Nancy" instead of "Donald" 

------------------------------------------------------------------------------------

Now I created another parameter

patients = ["John", "Donald", "Kevin", "Tony", "Alice"]
patients_number = [2,4,7,11,25]


when you print "patients", you're going to get just their names.. 

if you want to print all values that you made, you can use "extend" function.

extend function; it basically allows you to take the lists and append another lists on the end of it
so let's say patients.extend(patients_number)

and print(patients)

['John', 'Donald', 'Kevin', 'Tony', 'Alice', 2, 4, 7, 11, 25]
you get all the values.



You can also add a value individually by using "append" function 
patients.append("Tom")

and print.. 


['John', 'Donald', 'Kevin', 'Tony', 'Alice', 'Tom']
you see "Tom" is added individually.
 



If you want to add the value in the middle or something; you can use "insert" function 

First parameter is going to be the index where you want to put the item. 
patients.insert(1, "kay")

So this means that I want to put "kay" to index 1

print(patients)

['John', 'kay', 'Donald', 'Kevin', 'Tony', 'Alice']

Now you see "kay" is added into index 1 location. 


------------ you can also remove some items... 
patients.remove("Kevin")

print(patients)

['John', 'Donald', 'Tony', 'Alice']


If you want to remove everything ,, you can say
patients.clear()

print(patients)

[]

patients.pop()


and get nothing.


Also there's is "pop" function 
patients.pop()

you're going to get everything except for the last one.
['John', 'Donald', 'Kevin', 'Tony']

and you can put the number inside of pop if you want to pop for a certain index like pop(3), it's very similar to "remove" function. 




You can also bring index 
patients = ["John", "Donald", "Kevin", "Tony", "Alice"]


print(patients.index("Tony"))

and you get 3


Count number,,  I made four Tony on my lists.
patients = ["John", "Donald", "Kevin", "Tony", "Tony", "Tony", "Tony", "Alice"]


print(patients.count("Tony"))


you get "4" 


for Alphabetical order; you can use "sort" function
patients = ["John", "Donald", "Kevin", "Tony", "Tony", "Tony", "Tony", "Alice"]

patients.sort()

print(patients)

['Alice', 'Donald', 'John', 'Kevin', 'Tony', 'Tony', 'Tony', 'Tony']


For number, you're going to get Ascending order
patients_number = [22,5,7,1,87]

patients_number.sort()

print(patients_number)

[1, 5, 7, 22, 87]


you can Reverse,, as well
patients_number = [22,5,7,1,87]

patients_number.reverse()

print(patients_number)

[87, 1, 7, 5, 22]


for Copy
patients_number = [22,5,7,1,87]

patients2 = patients_number.copy()

print(patients2)

you get copied one 
[22, 5, 7, 1, 87]


This is how to make lists :) 

Popular posts from this blog

[Python] Dictionary

[Visual Design 2/3]

[JavaScript] For loop , Function