0% found this document useful (0 votes)
11 views13 pages

Python For Og Lecture 32 33 34 35 Lists

The document discusses Python lists, including how to create and access list elements, add and remove elements from lists, concatenate lists, and check if an element is present in a list using the in keyword. It provides examples of using list methods like append(), insert(), pop(), remove(), slicing, indexing, and the + and extend() operators for concatenation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views13 pages

Python For Og Lecture 32 33 34 35 Lists

The document discusses Python lists, including how to create and access list elements, add and remove elements from lists, concatenate lists, and check if an element is present in a list using the in keyword. It provides examples of using list methods like append(), insert(), pop(), remove(), slicing, indexing, and the + and extend() operators for concatenation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

2/2/2021 Python for O&G Lecture 32, 33, 34, 35 - Lists 1 - Colaboratory

Python for Oil and Gas

Website - https://petroleumfromscratchin.wordpress.com/

LinkedIn - https://www.linkedin.com/company/petroleum-from-scratch

YouTube - https://www.youtube.com/channel/UC_lT10npISN5V32HDLAklsw

# Data Structres helps you to store and access the data

# Built in data structures are Lists, Dictionaries, Sets and Tuples

Lists

1. Ordered collection of data


2. Can store any data type

# Syntax []
/
2/2/2021 Python for O&G Lecture 32, 33, 34, 35 - Lists 1 - Colaboratory
a = []

type(a)

list

# list with int values

pressure = [2000, 2200, 2500]

type(pressure)

list

# list with float values

mud_wt = [9.5, 10.2, 12.5]

type(mud_wt)

list

# list with string values

production = ['Maharashtra', 'Rajasthan', 'Andhra Pradesh']

# list with multiple data types

list1 = [2500, 12.5, 'Rajasthan', True, 5+4j]

type(list1)

list /
2/2/2021 Python for O&G Lecture 32, 33, 34, 35 - Lists 1 - Colaboratory

Lists Indexing

print(list1)

[2500, 12.5, 'Rajasthan', True, (5+4j)]

print(list1[0])

2500

print(list1[2])

Rajasthan

print(list1[4])

(5+4j)

Double-click (or enter) to edit

print(list1[-1])

(5+4j)

List Slicing

curves = ['IPR', 'TPR', 'WPR', 'CPR']

curves[0:2] # 0th, 1st, 2nd is not included


/
2/2/2021 Python for O&G Lecture 32, 33, 34, 35 - Lists 1 - Colaboratory
['IPR', 'TPR']

curves[2:4:1]

['WPR', 'CPR']

curves[1:]

['TPR', 'WPR', 'CPR']

curves[:3:2]

['IPR', 'WPR']

# Reverse order

curves[::-1]

['CPR', 'WPR', 'TPR', 'IPR']

updating an element in list

pressure = [1200, 2500, 30000]

# one way is to create a whole new list

# Second way is to just update the last element

pressure[2] = 3000

print(pressure)

/
2/2/2021 Python for O&G Lecture 32, 33, 34, 35 - Lists 1 - Colaboratory
[1200, 2500, 3000]

# let's say we want to change all the elements instead of 1st element

Lecture 33 - Adding Data to Lists

Adding data to a list

1. append method
2. insert method

# append method

# let's say we are inputting a info of resrvoir in a list [location of the reservoir ,porosity, perm in md, resrervoir pressure]

# But I have only included poroity and perm as of now

res_1 = [0.25, 30]

# add reservoir pressure

# Synatx ----> list_name.append(Whatever you want to add)

res_1.append(3000)

/
2/2/2021 Python for O&G Lecture 32, 33, 34, 35 - Lists 1 - Colaboratory

print(res_1)

[0.25, 30, 3000]

# insert method

# Allows us to add item in list based on index. means we can add the item wherever we want

# Syntax - list_name.insert(index where youu want to add, value which you want to add)

# add 'KG Basin' at 1st position

res_1.insert(0, "KG Basin")

print(res_1)

['KG Basin', 0.25, 30, 3000]

a = [1, 2]

a.append(3)

[1, 2, 3]

a.append('hey')

print(a) /
2/2/2021 Python for O&G Lecture 32, 33, 34, 35 - Lists 1 - Colaboratory

[1, 2, 3, 'hey']

a.insert(2, 2.5)

print(a)

[1, 2, 2.5, 3, 'hey']

Concatenate lists

1. use of + operator
2. extend method
3. append method

# Use of + sign

b = [1,2]
c = [3, 4]

# I need a list [1,2,3,4]

d = b + c

print(d)

[1, 2, 3, 4]

e = c + b

print(e)

[3, 4, 1, 2]

# extend method

/
2/2/2021 Python for O&G Lecture 32, 33, 34, 35 - Lists 1 - Colaboratory

# Syntax --> list_you_want_to_extend.extend(list you want to add to existing list)

x = [5, 6]
y = [7, 8]

x.extend(y)

print(x)

[5, 6, 7, 8]

# let us try append method for concatenating the lists

x = [5, 6]
y = [7, 8]

x.append(y)

print(x)

[5, 6, [7, 8]]

Assignment 11

# [first_name, last name, [age, a hobby]] Final result

# Step 1 --- [last_name]


# Step 2 --- [fist_name, last_name]
# Step 3 --- [first_name, last name, [age, a hobby]]

/
2/2/2021 Python for O&G Lecture 32, 33, 34, 35 - Lists 1 - Colaboratory

Lecture 34 - Deleting data from lists

Common ways to delete the data from list

# I want to create a list of density reducing elements used in cement

den_red = ['Bentonite', 'Diatomaceous Earth', 'Barite', 'Glisonite', 'Pozzolan', 'Hametite']

# But we know that barite and hametite aere density increasing additives, we want to remove them

pop method

# pop method

# syntax - list_name.pop() -> remove the last element from list

den_red.pop()

'Hametite'

print(den_red)

['Bentonite', 'Diatomaceous Earth', 'Barite', 'Glisonite', 'Pozzolan']

# now we also want to remove Barite. Barite is at index 2


/
2/2/2021 Python for O&G Lecture 32, 33, 34, 35 - Lists 1 - Colaboratory

# list_name.pop(index of item you want to remove)

den_red.pop(2)

'Barite'

print(den_red)

['Bentonite', 'Diatomaceous Earth', 'Glisonite', 'Pozzolan']

del operator

# del operator

den_inc = ['Barite', 'Hametite','Bentonite', 'Ilemite'] # list of density increasing elements in cementing

# SYNTAX - del list_name[index of item you want to remove]

del den_inc[2]

print(den_inc)

['Barite', 'Hametite', 'Ilemite']

remove method

# remove method

# til now in both the methds pop and del we removed the elements based on their index
# If we know the element's names and want to delete them by their name, we use remove method

# let us take an example of ship's motions

3 transitions motions - surge, sway , heave


3 rotational motions - roll, pitch, yaw /
2/2/2021 Python for O&G Lecture 32, 33, 34, 35 - Lists 1 - Colaboratory

trans_motion = ['surge', 'pitch', 'sway', 'roll', 'heave', 'yaw']

# Syntax - list_name.remove('element name')

trans_motion.remove('roll')

print(trans_motion)

['surge', 'pitch', 'sway', 'heave', 'yaw']

trans_motion.pop()

'yaw'

print(trans_motion)

['surge', 'pitch', 'sway', 'heave']

trans_motion.remove('pitch')

print(trans_motion)

['surge', 'sway', 'heave']

# We can only give one argument at a time in remove method

trans_motion = ['surge', 'pitch', 'sway', 'roll', 'heave', 'yaw']

trans_motion.remove('pitch', 'roll', 'yaw')

/
2/2/2021 Python for O&G Lecture 32, 33, 34, 35 - Lists 1 - Colaboratory

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-70-3a554e4caf45> in <module>()
5
6
----> 7 trans_motion.remove('pitch', 'roll', 'yaw')
8
9

TypeError: remove() takes exactly one argument (3 given)

SEARCH STACK OVERFLOW

Lecture 35 - in keyword with lists

in keyword with list

favs = ['Rockstar', 'ZNMD', 'Barfi', 'Tamasha']

if 'Rockstar' in favs:
print('Rockstar is one of my fav movies')
else:
print('Rockstar is not there is list')

Rockstar is one of my fav movies

favs = ['Rockstar', 'ZNMD', 'Barfi', 'Tamasha', 'Soul']

if 'Soul' in favs:
print('Soul is one of my fav movies')
else:
print('Soul is not there in list')

/
2/2/2021 Python for O&G Lecture 32, 33, 34, 35 - Lists 1 - Colaboratory

Soul is one of my fav movies

You might also like