Lists in Python


Create a list
Simple list
mylist = [“apple”, “orange”, “banana”, “mango”, “pineapple”]
mylist[2] # returns ‘banana’
mylist[-1] # returns ‘pineapple’
mylist[2:4] #returns ‘banana’, ‘mango’, ‘pineapple
if “apple” in thislist:
  print(“Yes, ‘apple’ is in the fruits list”)
mylist.insert(2, “watermelon”) #insert at position speified
mylist.append(‘grapes’) #adds to end of list
mylist = [“apple”, “banana”, “cherry”]
tropical = [“mango”, “pineapple”, “papaya”]
mylist.extend(tropical) # adds tropical list to thislist
my.remove(“banana”) #removes first occurrence
mylist.pop(1) #removes specified instance
del mylist[0] #also removes instance
del mylist # deletes list
clear(mylist) # clears the list

for i in thislist: #look list
  print(i)

i = 0
while i < len(thislist): #while loop
  print(thislist[i])
  i = i + 1

newlist = []
for x in fruits: #adds list items containing a to new list
  if “a” in x:
    newlist.append(x)

thislist.sort() #sort alphabetically or numerically depending on data type
thislist.sort(reverse = True) #sort in reverse
thislist.sort(key = str.lower) #sort lower case first
thislist.reverse() #sort in reverse
mylist = thislist.copy() #copy list
mylist = list(thislist) #also makes a list
list3 = list1 + list2 #concetenate lists





6 rows, 2 columns

b1 = [[2308, 6], [2408, 6.2], [2508, 5.8],[2608, 5.6], [2708, 5.9]] #create the list
print(b1) #print the list
print(len(b1)) #print length  of list
print(type(b1)) #print data type of list
print(b1[:2]) #print the first 2 elements in the list (note doesn’t use zero)
print(b1[3:]) #print from the 3rd elements and after

Reference Index in array

b1[3] #returns second element
b1[0:2] # returns first and second element of list
b1[-1] # returns last element index

Add and Delete, Update
Delete
del(b1[0]) # delete first element
or
b1.pop(1) #remove second element


Insert
b1.insert(1,[24082, 111]) #insert element at position 1
Update
b1[6]= [2808,6.7] #update value

Append
Additions = [[29008, 6.6], [3008, 6], [3108, 6.1]]
b1.extend(Additions) #Adds the Additions list to b1
b1.append([2808,6.6]) # add 1 element (only 1)

Clear the list
b1.clear() #empties the list

Check if element in array

if [2308,6.2] in b1: print(“yes”)
Loop
for x in b1: print(x)

def country_select(countrylist):
count = 1
for i in countrylist:
print(f”{i}: {countrylist[i]}”)
count = count + 1
return countrylist



Numpy

baseball = [180, 215, 210, 210, 188, 176, 209, 200]
import numpy as np
np_baseball = np.array(baseball)
print(type(np_baseball))
mean = np_baseball.mean()
print(“mean is: “+ str(mean))
med = np.median(np_baseball)
print(“median is: “+str(med))
std = np.std(np_baseball)
print(“standard deviation is: “+str(std))


Comments

2 responses to “Lists in Python”

  1. Lois Sasson Great information shared.. really enjoyed reading this post thank you author for sharing this post .. appreciated

  2. Smartcric very informative articles or reviews at this time.

Leave a Reply

Your email address will not be published. Required fields are marked *