#πŸ”’ Help with sorted()

70 messages Β· Page 1 of 1 (latest)

clever wolf
#

I'm having trouble figuring out why this isn't functioning as expected.

customer_data = [["Ainsley", "Small", True], ["Ben", "Large", False], ["Chani", "Medium", True], ["Depak", "Medium", False]]
customer_data_final = customer_data + [
["Amit", "Large", True],
["Karim", "X-Large", False]]

Provides list of customers

print ("Here is a list of all of your customers:" + "\n" )
for customer in customer_data_final:

print(sorted(customer[0]))

hearty raftBOT
#

@clever wolf

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

clever wolf
#

Particularly the last three lines. this is the output. I expected each name to be sorted, not the letters of each name. what can i do?

split tulip
#

print(sorted (customer)[0])

torn meteor
split tulip
#

Wait

#
customer_list.sort()
#

That should do my the trick

#

Noo

#

I am stupid what am I doing, sorry

torn meteor
#

no why? that sounds good. but it's in-place.

split tulip
#

Nope the name is in the first item of list

#

I am really tired, sorry

rotund hull
#

Then you need to pass a key to your sorting function that fetches the first item in your lists.

torn meteor
rotund hull
#

ah yeah, right

clever wolf
#

in simple terms please πŸ™

split tulip
#

You should use a dictionary, instead

#

Really

mellow raven
torn meteor
#

sorted(customer_data_final)

mellow raven
#

and what u where probably trying is expressed as
sorted(customer, key= lambda x: x[0])

rotund hull
#

And if you only want the names then retrieve them afterwards by indexing your sublists.

clever wolf
torn meteor
#
for customer in sorted(customer_data_final):
    print(customer)```
clever wolf
split tulip
#

it just means it will sort by the first item in each list

mellow raven
clever wolf
mellow raven
#

lambda x: x[0] is just a short way to describe a function returning the first element of an iterable

split tulip
#

!eval```py
test=[["a", "z"],
["b", "y"]
]
print(sorted(test, key=lambda x: x[0]))
print(sorted(test, key=lambda x: x[1]))

hearty raftBOT
mellow raven
#

basically u sort
[key(i) for i in list]

clever wolf
mellow raven
#

just get the corresponding list values not the key(element) values

mellow raven
clever wolf
#

ohhh

#

ok

mellow raven
#

if u want to sort based on length can u tell me what key u need? (just to check if u got these keys)

split tulip
#

If the key is x[1] it will be sorted by Small, Medium, Large ect. Alphabetically

torn meteor
#

If no key is specified, it will sort by name, then by size (when name is equal), then by expedited (when size is equal)

mellow raven
split tulip
#

Not literally

#

It will not sort by literally meaning but the alphabetical order

#

Apologies for the ambiguity

mellow raven
#

oh wait i got it we arnt talking about single lists but nested ones right?

split tulip
mellow raven
split tulip
torn meteor
split tulip
split tulip
mellow raven
#

u could sort by size with the key
lambda x: ["Small","Medium","Large"].index(x[1])

split tulip
#

I don't see why op isn't using dictionary, as using dictionary allow indexing with name

#

You can then skip looping through the whole list to find a person's data

split tulip
clever wolf
split tulip
torn meteor
split tulip
hearty raftBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.