1 / 5

Concatenate(join together) lists

Concatenate(join together) lists. list1 = ["gold", "gems ", "diamonds "] list2 = [“rubies", “emeralds ", “opals"] list3 = list1 + list2 print(list3) list1 +=list2 print(list1). Lists (Stuff we can do to lists). listofstuff = [“ant", “bear", “cat", “ dog“,”elephant ”] # assign by index

liona
Download Presentation

Concatenate(join together) lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Concatenate(join together) lists list1 = ["gold", "gems ", "diamonds "] list2 = [“rubies", “emeralds ", “opals"] list3 = list1 + list2 print(list3) list1 +=list2 print(list1)

  2. Lists (Stuff we can do to lists) listofstuff = [“ant", “bear", “cat", “dog“,”elephant”] # assign by index listofstuff[0] = “aardvark" print(listofstuff) # assign by slice listofstuff[3:5] = [“dolphin"] print(listofstuff) # delete an element del listofstuff[2] print(listofstuff) # delete a slice del listofstuff[:2] print(listofstuff)

  3. #remove a score • score = 8 • if score in scores: • scores.remove(score) • print (scores) • scores.reverse() • print(scores) • # sort scores • scores.sort() • print(scores) • scores.sort(reverse = True) • print(scores) scores = [] #add a score newscore = 3 #append - adds to list scores.append(newscore) newscore = 8 scores.append(newscore) newscore = 4 scores.append(newscore) newscore = 2 scores.append(newscore) newscore = 5 scores.append(newscore) newscore = 8 scores.append(newscore) print(scores)

  4. Other methods available for lists • scores.append(3) • scores.append(8) • scores.append(3) • print(scores) • print(scores.count(3)) • print(scores.index(3)) • scores.insert(7, 4) • print(scores) • scores.pop() • print(scores) • scores.remove(5) • print (scores) count(value) – counts the number of times value occurs in list index(value) – returns index of first occurrence of value insert(f,value)- inserts value at position f pop([f]) – returns value at position f and removes value from list. Without f, it pops the last element off the list remove(value) – removes first occurrence of value from the list

  5. Generating Random Numbers import random def sl(list,new): if len(list) == 1: new.append(list.pop( )) return(new) else: x = random.randrange(0,len(list)) new.append(list.pop(x)) return(sl(list,new)) list = ["m","o","t","h","e","r"] new = [] print(sl(list,new))

More Related