This question already has answers here:
Print list without brackets in a single row
(14 answers)
Closed 5 months ago.
For some reason when I print a list like
list = []
list.append("0")
list.append("1")
print(list[0])
the output will be ["0"]
My actual code is a large block of text. Here's a link to the actual code: https://pastebin.com/Z54NfivR
Try this:
print(*list)
This essentially unpacks your list and its elements are treated as if they were separated by commas in the print function.
I used the name list because that was included in your example but it is a good practice to avoid using python commands as variable names.
Related
This question already has answers here:
How to get first AND last element of tuple at the same time
(4 answers)
Closed 1 year ago.
Is there a function in Python to do it?
I have tried printing out last item in the tuple, but each time I want to generate more strings, I have to change it manually.
Try tuple_name[-1]
replace tuple_name with your tuple name
finally, it will be like print(tuple_name[-1])
This question already has answers here:
Filtering a list of strings based on contents
(5 answers)
Closed 3 years ago.
I am trying to filter a list in python.
What i need is a way to limit what is beeing printed out from the list
Example:
I want to find all subjects containing "ECON"
from this list:
List = ["INFO100","INFO104","INFO110","INFO150","INFO125","ECON100", "ECON102"]
And i want to be able to print out the full name of the objects containing "ECON" (that means i want it to return "ECON100", "ECON102")
is there an easy way to do this?
for sub_string in List:
if "ECON" in sub_string:
print(sub_string)
This question already has answers here:
Convert a list with strings all to lowercase or uppercase
(13 answers)
Closed 6 years ago.
I am trying to read a text file in jupyter notebook and fetch the unique words in the file.
I am reading that file as a list and then trying to apply lower case on it. But the .lower() function doesn't work with a list. Please help with the same.
With a list of values in val you can do this:
valsLower = [item.lower() for item in vals]
This question already has answers here:
Python Sort() method [duplicate]
(3 answers)
Closed 6 years ago.
Hi I am new to programming and I am trying to write a program that takes a list of student names and sorts them to create a class roll. The list of names will be given on a one line separated by a single space. It is in alphabetical order
This is what I want the output to looks like.
This is my current code below and I am not sure why it keeps coming up with errors.
names = input('Students: ')
print('Class Roll')
output = names.sort()
print(output)
You never split the input apart so there isn't anything to sort
names = names.split()
names.sort()
Here are a few problems I see with your code
The input is read as a trying, you need to split this into individual names
sort works inplace, it returns None
You can do the following
Modify the input command as names = input('Students: ').split(' ')
print(names) instead of output
EDIT
To print them in the manner shown in the question do
for name in names:
print(name)
This question already has answers here:
Python- Turning user input into a list
(3 answers)
Create a tuple from an input in Python
(5 answers)
Closed 10 months ago.
Write a Python program which accepts a sequence of comma-separated numbers from user and generate a list and a tuple with those numbers.
values = input("Input some comma separated numbers : ")
list = values.split(",")
tuple = tuple(list)
print('List : ',list)
print('Tuple : ',tuple)
This does work but is there any other easier way?
If you're looking for a more efficient way to do this, check out this question:
Most efficient way to split strings in Python
If you're looking for a clearer or more concise way, this is actually quite simple. I would avoid using "tuple" and "list" as variable names however, it is bad practice to name variables as their type.
Well, the code that you have written is pretty concise but you could remove few more line by using the below code:
values = input("Enter some numbers:\n").split(",")
print(values) #This is the list
print(tuple(values)) #This is the tuple