Printing all string elements in list except for one in Python

remove all strings from list python
python remove string from list if contains substring
python remove multiple items from list by index
remove element from list python
python remove item from list if contains
python select from list
python list except* one element
python list remove by index

So I'm making a scheduling program that randomly schedules NHL teams. I have a mini list of teams from the Atlantic Division:

atl_div = ["BOS", "BUF", "DET", "FLA"]

When a user enters a team abbreviation, they get the elements printed to them, minus the team in the list, for example if I entered BUF, the user would get back:


I tried

input_team = input("Enter a team abbrev.:")

if input_team == "BUF":

but that won't work, since ~ is used on numbers and not lists.

I have to admit I'm a bit of a novice programmer, so I apologize if it sounds that way in my question. We all start somewhere :)

Thank you for any help and feel free to ask for more details if necessary!

input_team = input('Enter a team abbrev.:')
print('\n'.join([team for team in atl_div if team != input_team]))

EDIT: Updated print formatting

Index all *except* one item in python, For a list, you could use a list comp. For example, to make b a copy of a without the 3rd element: a = range(10)[::-1] # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] b = [x for i,x in� Printing a list in python can be (list) and print all elements of the list one by one Convert a list to a string for display : If it is a list of strings

A simple list comprehension will produce a new list without the undesired element:

print(*[team for team in atl_div if team != input_team], sep="\n")

That makes a new list temporarily with all elements except those equal to input_team. The * unpacks it as sequential positional arguments to print, and sep="\n" tells print to put a newline between each element when printing. If you want to permanently remove the element in question from the list, you can instead do:

print(*atl_div, sep="\n")

Note that the behavior will differ a bit if there isn't exactly one entry matching input_team; remove will throw an exception if no matching element exists, and it will only remove one copy if there is more than one instance of the value in the list. The list comprehension will silently remove all copies of the value, and doesn't care if there weren't any to start with.

Index all *except* one item in python, take(range(i)+range(i+1 Python | Print list after removing element at given index. Given an index, remove the element at that index from the list and print the new list. Examples: Using traversal in the list, append all the index values except the given index to a new list and then print the new list. Python Program to return Elements in a List Example 2. This python program is the same as above. But this time, we are using For Loop to iterate every element in a list, and printing that element.

Two easy ways you can do this:

The simplest way would be to go one by one through the list, and print any one that is not the string you are explicitly looking not to pick.

for team in atl_div:
    if input_team != team:

An arguably more Pythonic way of doing the same thing, although not as efficient (with a small list like this efficiency is not as important) you can do a list comprehension, and make a list like. This way, you're going to make a new list, which doesn't have the team you're looking for.

included_teams = [x for x in atl_div if x != input_team]

You can then print out that list, element by element:

for team in included_teams:

This can be simplified to the following, perhaps slightly less readable version of that same problem:

for team in [x for x in atl_div if x != input_team]:

In either of these solutions, you can be more forgiving, and apply string transforms like .upper() to the input_team to allow it so that a user who enters buf would still get the expected list, BOS, DET, FLA.

With the first answer, this can be done like so:

for team in atl_div:
    if input_team.upper() != team:

Python : How to remove element from a list by value or Index , Python's list provides a member function to remove an element from list i.e. print("Given Element Not Found in List") pop() can throw error if given index is out of range, so use try / except Python Dictionary: pop() function & examples � Python: Remove characters from string by regex & 4 other ways� Printing list elements in Python: Here, we are going to learn how to print list elements in different ways? Submitted by IncludeHelp, on April 09, 2019 . In this program – we are going to learn how can we print all list elements, print specific elements, print a range of the elements, print list multiple times (using * operator), print multiple lists by concatenating them, etc.

If you want to use pandas you can do this:

import pandas as pd
aa = pd.Series(["BOS", "BUF", "DET", "FLA"]) 
aa[aa != 'BOS'].tolist()
# ['BUF', 'DET', 'FLA']

Python Lists and List Manipulation | by Michael Galarnyk, The list defined above has items that are all of the same type (int), but all the items of a list do not need to be of the same type as you can see below. The list contains an int, a bool, a string, and a float. Output of accessing the last item in the List The code below returns a list with items from index 1 to the end of the list Previous: Write a Python program to generate and print a list of first and last 5 elements where the values are square of numbers between 1 and 30 (both included). Next: Write a Python program to generate all permutations of a list in Python.

How to index every element in a list except one in Python, Indexing every element in a list except one returns all of the elements in the For example, indexing every element of [1, 2, 3, 4] except for the value at index 2 returns [1, 2, 4] . + a_list[excluded_index+1:]. print(indexed_list). Output. [1, 2, 4 ]� Previous: Write a Python program to create a histogram from a given list of integers. Next: Write a Python program to print out all even numbers from a given numbers list in the same order and stop the printing if any numbers that come after 237 in the sequence.

18 Most Common Python List Questions, Discover how to create a list in Python, select list elements, the With this blog post, DataCamp wants to help you to tackle one topic, print(isinstance(0.125, collections This operation allows you to glue together all strings in your list Note that besides list comprehensions, also set and dictionary� Print All Element Except Last To print all the elements of the list variable in Python. You have to use the below-given example which removes the last element. Add -1 after the len() function of Python to remove the last element from the output. You will get all the elements of the list element except the last one.

array — Efficient arrays of numeric values — Python 3.8.5 , Arrays are sequence types and behave very much like lists, except that the type of objects stored in them is constrained. A string with all available type codes. In this step-by-step tutorial, you'll learn about the print() function in Python and discover some of its lesser-known features. Avoid common mistakes, take your "hello world" to the next level, and know when to use a better alternative.

  • Note that this will print a string representation of a list, which is different than the output format expected by the OP.