Python Forum
Intro python using lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Intro python using lists
#1
This is a homework question. I am learning how to use lists, slicing, del, sort, and other functions. I'm not asking anyone to do this for me, I just need some help understanding what to do in regards to question number 7.

'''1. Create the following list of animals (in this order): zebra, cobra, stork, penguin, shark, lion, buffalo, whale, seal, eagle, wren, horse, rat

Print to see the list.

2. Using list methods, add the following to the end of the list: rattlesnake, ape, crow, tuna (extend)

Print to see the list.

3. Using list methods, change the item at index 5 to elephant.

Print to see the list.

4. Using list methods, insert between whale and seal, wolf

Print to see the list.

5. Remove wren from the list using list methods, functions or statements (del)

Print to see the list.

6. Sort the list alphabetically using methods, this should be permanent, not temporary. (sort) method

Print to see the list.

7. Using the methods or functions learned in class, remove the birds from the list and add those birds a new list that you create.
#3,4,8,12

Print to see the original list without birds and the new "birds" list.

8. Slice the last 5 animals from the list and print those 5 in a for loop with the text "The _____ is an animal", where _____ is each individual animal. (del)'''
animals = ['zebra', 'cobra', 'stork', 'penguin', 'shark', 'lion', 'buffalo', 'whale', 'seal', 'eagle', 'wren', 'horse']
print(animals)
animals.extend(['rattlesnake','ape', 'crow', 'tuna'])
print(animals)
animals[5] = 'elephant'
print(animals)
animals.insert(8, 'wolf')
print(animals)
del animals[11]
print(animals)
animals.sort()
print(animals)
numbers = [3,4,7,11]
bird = [animals[val] for val in numbers]
print(bird)
animals = [animals[val] if val is=false]
print(animals)
so far I have completed all of the questions up to #6 relatively easily with the outputs being:
1. ['zebra', 'cobra', 'stork', 'penguin', 'shark', 'lion', 'buffalo', 'whale', 'seal', 'eagle', 'wren', 'horse']
2. ['zebra', 'cobra', 'stork', 'penguin', 'shark', 'lion', 'buffalo', 'whale', 'seal', 'eagle', 'wren', 'horse', 'rattlesnake', 'ape', 'crow', 'tuna']
3. ['zebra', 'cobra', 'stork', 'penguin', 'shark', 'elephant', 'buffalo', 'whale', 'seal', 'eagle', 'wren', 'horse', 'rattlesnake', 'ape', 'crow', 'tuna']
4. ['zebra', 'cobra', 'stork', 'penguin', 'shark', 'elephant', 'buffalo', 'whale', 'wolf', 'seal', 'eagle', 'wren', 'horse', 'rattlesnake', 'ape', 'crow', 'tuna']
5. ['zebra', 'cobra', 'stork', 'penguin', 'shark', 'elephant', 'buffalo', 'whale', 'wolf', 'seal', 'eagle', 'horse', 'rattlesnake', 'ape', 'crow', 'tuna']
6. ['ape', 'buffalo', 'cobra', 'crow', 'eagle', 'elephant', 'horse', 'penguin', 'rattlesnake', 'seal', 'shark', 'stork', 'tuna', 'whale', 'wolf', 'zebra']
#I also have part of #7 complete with adding the birds to a new list with the output being:
7. ['crow', 'eagle', 'penguin', 'stork']
I'm just not sure on how or if I can remove the birds from the first list and make or add them into a second list. I know I could go through and delete the birds specifically one at a time from the "animals" list, but is there a faster way to do so, or as one command? With "animals = [animals[val] if val is=false]" am I on the right track with what I have, or do I need to try something else?
Reply
#2
Use the .pop() command to removea bird or what ever is in the list.
Like so:
m = [1,2,3]
two = m.pop(1)
print(two)
Output:
2
Edit:
Looking at your problem 7, I don`t know how to do it in 1 loop, you can do it in 2 loops like so:
animals = ['zebra', 'cobra', 'stork', 'penguin', 
			'shark', 'lion', 'buffalo', 'whale',
			 'seal', 'eagle', 'wren', 'horse']
bird_nums = (2,3,9,10)

birds = []
for bird_num in bird_nums:
	birds.append(animals[bird_num])
for bird in birds:
	animals.remove(bird)

print("birds:",birds)
print("animals:",animals)
Output:
birds: ['stork', 'penguin', 'eagle', 'wren'] animals: ['zebra', 'cobra', 'shark', 'lion', 'buffalo', 'whale', 'seal', 'horse']
Reply
#3
Line 16 is close but needs a fix. A list comprehension on the order of
no_foul = [x for x in animals if x (you choose the words) bird]
print(no_foul)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star Interesting Intro to python problem I can't solve. Honestworker 5 12,160 Mar-04-2021, 02:05 AM
Last Post: BashBedlam
  Intro to Python for Data Science with Datacamp Sabrusura 4 3,574 Aug-21-2018, 09:03 AM
Last Post: Sabrusura

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020