Sorting a list of students [duplicate] - python

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)

Related

remove the [''] when printing a list in python [duplicate]

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.

Filter list and print whole name containing string python [duplicate]

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)

Having problems how to use an if statement to determine if something is in a list or not. Python [duplicate]

This question already has answers here:
Python check if Items are in list
(5 answers)
Is there a short contains function for lists?
(6 answers)
Closed 5 years ago.
I am writing a program that takes a blank input and does various things with it. The input "show me contact _____" (With the ____ being filled in with somebody's name). The names are stored in a list and I need it to be able to check if the name is in the list, and if it is, print the contact information. I figured an if statement would be the best way to do it, but I cannot figure out how.
Edit: I have tried using in, but my problem is that the input contains "show me contact _____" and not just the name. This causes this:
names = ['john doe','john doe','john doe']
user_input = input('>')
if user_input in names:
print(email)
to not work.
You want
if user_input in names:
When used with a list, in checks for membership. When used with a string, it will check for substrings.

Turning Input into a list [duplicate]

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

Assign a list to variables [duplicate]

This question already has answers here:
How to expand a list to function arguments in Python [duplicate]
(4 answers)
Closed 10 months ago.
I have a python list:
x = ['aa', 'bb', 'cc']
The len() (or list length, which is 3 in this case) of this list can be any number, as it is basically coming from database. My question is: how can I assign each string member of this list into a separate variable automatically? The point over here is: I do not know the number of elements in the list, since they are always different.
Once this is resolved, I am trying to put it into a Python Google Charting (GChartWrapper's pie3d chart) function like this:
G.label(aa,bb,cc)
However, if I simply put the list in like:
G.label(x)
then it is naming only one section of the pie chart as the complete list.
You're doing it wrong.
G.label(*x)

Categories