3 - Introducing lists

Welcome file

What is a list ?

A list is a colllection of items in a particular order. You can put anything you want into a list, and the items in your list don’t have to be related in any particular way. Because they usually contains more than one element.

In Python, square brackets [] indicate a list, and individual elements in the list are seperated by commas.

Code :
bicycles = ['trek','cannodale','redline','specialized']
print(bicycles)
Output :
['trek', 'cannodale', 'redline', 'specialized']
[Finished in 0.1s]

Accessing Elements in a list

How to get first item on this list?
Code :

bicycles = ['trek','cannodale','redline','specialized']
print(bicycles[0])

Output :

trek
[Finished in 0.1s]

Can i use Strings methods from python3 varibles and simple data types on any elements ?

Answer is simple. Yes u can.
Code :

bicycles = ['trek','cannodale','redline','specialized']
print(bicycles[0].title())
print(bicycles[0].upper())
print(bicycles[0].lower())

Output :

Trek
TREK
trek
[Finished in 0.1s]

Index positions start at 0 Not a 1

Python considers the first item in a list to be at position 0, not position 1. The reason has to do with how the list operations are implemented at a lower level.
About index errors

  • -1 = First item from end
  • 0 = First item on list
  • 1 = Second item on list
bicycles = ['trek','cannodale','redline','specialized']
print(bicycles[-1])
print(bicycles[0])
print(bicycles[1])
specialized
trek
cannodale
[Finished in 0.1s]

This syntex is quite useful, Because you’ll often want to access the last items in a list without knowing exactly how long the list is. This convention extends to other negative index values as well.

Using Individual values from a list

You can use individual values from a list trust as any other varrible, You can use f-strings to create a message based on a value from a list.

Code :

bicycles = ['trek','cannodale','redline','specialized']
message = f"My first bicycle was a {bicycles[-1].title()}"
message1 = f"My first bicycle was a {bicycles[0].title()}"
message2 = f"My first bicycle was a {bicycles[1].title()}"
print(message)
print(message1)
print(message2)

Output :

My first bicycle was a Specialized
My first bicycle was a Trek
My first bicycle was a Cannodale
[Finished in 0.1s]

Modifying elements in a List

To change an element, Use the name of the list followed by the index of the element you want to change, and then provide the new value you want that item to have.

For example, let’s say we have a list of bicycles, and the first item in the list is ‘honda’. How would we change the value of this first item?

bicycles = ['trek','cannodale','redline','specialized']
print(bicycles)
bicycles[0] = 'dsi'
print(bicycles)
['trek', 'cannodale', 'redline', 'specialized']
['dsi', 'cannodale', 'redline', 'specialized']
[Finished in 0.1s]

Adding elements to a List

You can use two methods :

  1. append() - adding elements to end of the list
  2. insert() - add a new elements to any position in your list

  • Append method makes it easy to build lists dynamically. For example, You can start with a empty list and then add a items to the list using a series of append() calls.
motorcycles = []
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
print(motorcycles)
Output :
['honda', 'yamaha', 'suzuki']
[Finished in 0.1s]

  • You can add a new element at any position in your list by usingthe insert() method. You do this by specifying the index of the new element and the value of the new item.
motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.insert(0,'ducati')
print (motorcycles)
Output :
['ducati', 'honda', 'yamaha', 'suzuki']
[Finished in 0.1s]

Removing Elements from a List

You can use three methods to do this :

  1. del() - For known position (If you know the position of the item you want to remove from a list, you can use the del statement.)
  2. pop() - Use the value of an item after you remove it from a list (For example, you might want to remove a user from a list of active members and then add that user to a list of inactive members.)
  3. remove() - for unknown position (If you only know the value of the item you want to remove, you can use the remove() method)

If you’re unsure whether to use the del statement or the pop() method,
here’s a simple way to decide: when you want to delete an item from a
list and not use that item in any way, use the del statement; if you want
to use an item as you remove it, use the pop() method.

How to use these methods?

  1. del() - del motorcycles[0]
  2. pop() - popped_motorcycles = motorcycles.pop()
    {This will remove last item on list , But u can put a index in to brackets and change the position}
  3. remove() - motorcycles.remove(‘ducati’)
    or you can set a list and use remove method.
    too_expensive = ‘ducati’
    motorcycles.remove(too_expensive)

The remove() method deletes only the first occurrence of the value you specify. If there’s a possibility the value appears more than once in the list.You need to use loop.

Exercises in this chapter - Introducing lists - Exercise 1

Organizing a List

  • Sorting a List permanently with the sort() Function
  • Sorting a List temporarily with the sorted() Function
  • Reverse the orginal order with the reverse() Function
  1. sort() Function - Python’s sort() method makes it relatively easy to sort a list. To keep the task simple, let’s assume that all the values in the list are lowercase.
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort()
print(cars)
cars.sort(reverse = True)
print(cars)
Output :
['audi', 'bmw', 'subaru', 'toyota']
['toyota', 'subaru', 'bmw', 'audi']
[Finished in 0.1s]
  1. sorted() Function - The sorted() function let you display your list in a particular order but doesn’t affect the actual order of the list.
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(sorted(cars))
print(sorted(cars, reverse = True))
Output :
['audi', 'bmw', 'subaru', 'toyota']
['toyota', 'subaru', 'bmw', 'audi']
[Finished in 0.1s]

NOTE :Sorting a list alphabetically is a bit more complicated when all the values are not in lowercase. There are several ways to interpret capital letters when determining a sort order, and specifying the exact order can be more
complex than we want to deal with at this time

  1. reverse() Function - reverse() doesn’t sort backward alphabetically; it simply
    reverses the order of the list.The reverse() method changes the order of a list permanently, but you can revert to the original order anytime by applying reverse() to the same list a second time.
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(cars)
cars.reverse()
print(cars)
['bmw', 'audi', 'toyota', 'subaru']
['subaru', 'toyota', 'audi', 'bmw']
[Finished in 0.1s]

Finding a length of a list

You can quickly find the length of a list by using the len() function.

cars = ['bmw', 'audi', 'toyota', 'subaru']
print('I own',len(cars), 'cars')
Output :
I own 4 cars
[Finished in 0.1s]

Exercises in this chapter - Introducing lists - Exercise 2

Popular posts from this blog

Juicy Details - TryHackMe

Mustacchio - TryHackMe

Tech_Supp0rt: 1 - TryHackMe