#πŸ”’ Hey I have a problem with multiple list.append

65 messages Β· Page 1 of 1 (latest)

proud girder
#

what I have is
prices = [849, 850, 50, 880, 35, 460]
names = ["'sgNARC'", "'bladi.kery'", "'Sorainen'", "'MadMuesli'", "'Varjump'", "'Deaths_Mistress'"]
so what I want is
pri =
[ 850 = [user1]]
[ 550 = [user5]]
[ 450 = [user3]]
]
and the first numbers should be int so that I could sort them

oak jewelBOT
#

@proud girder

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.

lofty flume
#

WHat are user1 et al?

proud girder
lofty flume
#

I don't see your 550 price anywhere.

proud girder
lofty flume
#

Can you describe where the usernames come from, and to which prices they should be associated?

lofty flume
proud girder
#

alr wait a sec

#

[849, 850, 50, 880, 35, 460]
["'sgNARC'", "'bladi.kery'", "'Sorainen'", "'MadMuesli'", "'Varjump'", "'Deaths_Mistress'"]
849: sgNARC
850: bladi.kery
50: Sorainen

#

and so on

#

they are in order to each other rn

lofty flume
#

!d zip

oak jewelBOT
#
zip

zip(*iterables, strict=False)```
Iterate over several iterables in parallel, producing tuples with an item from each one.

Example:

```py
>>> for item in zip([1, 2, 3], ['sugar', 'spice', 'everything nice']):
...     print(item)
...
(1, 'sugar')
(2, 'spice')
(3, 'everything nice')
```...
proud girder
#

[849, 850, 50, 880, 35, 460]
["'sgNARC'", "'bladi.kery'", "'Sorainen'", "'MadMuesli'", "'Varjump'", "'Deaths_Mistress'"]

#

849: sgNARC
850: bladi.kery
50: Sorainen

lofty flume
#

print(list(zip(prices,names)))

proud girder
#

alr so now I have this

(33, "'FungalToenail'")
(50, "'Sorainen'")
(880, "'MadMuesli'")
(35, "'Varjump'")
(460, "'Deaths_Mistress'")

#

but can I sort it?

elfin raft
#

you assign that print to a var instead and use var = var.sort()

scenic rapids
#

if you use .sort() then no var =

#

it returns None

elfin raft
#

acutally im wrong in many ways.. there tuples no? so,
tuplevar = sorted(tuplevar)
i think

#

or is it a list of tuples.

scenic rapids
#

if you use sorted then you don't need to convert it to a list

proud girder
elfin raft
#

i meant structure wise,
[(,),(,)]

scenic rapids
scenic rapids
proud girder
elfin raft
#

i think he meant asc or desc

proud girder
#

asc

scenic rapids
scenic rapids
proud girder
lofty flume
#

So eg [(850,"A"),(850,"B")] may or may not get rearranged.

proud girder
scenic rapids
lofty flume
#

Note that there are at least 3 reasonable responses to this question:

  • for the same price, order by the user name
  • for the same price, do not move the users around
  • I don't care!
    This is a policy decision for you, and you can say the order within a single price is unimportant.
lofty flume
#

... or maybe it is important. But if you haven't thought about this detail, probably it is, in fact, unimportant.

scenic rapids
# proud girder yeah

then what Py.noob said will work. Use the sorted function on the result of zip to get a sorted list of (price, name)

lofty flume
#

The sorted() function can accept a key= parameter which you can use to govern how things are sorted.

Lacking that, sorted uses the natural comparison of the objects, ascending. For a 2-tuple, the first elements are compared and if they are the same, the second elements are compared.

So you'll get your desired behaviour for free with no key= parameter in this case.

proud girder
scenic rapids
#

wrap it around the zip

proud girder
#

everything I do gives an error and does not print anything

proud girder
#

(35, "'Varjump'")
(35, "'ezolofw'")
(36, "'xMorg'")
(36, "'xristosmtr'")
(50, "'Sorainen'")
(460, "'Deaths_Mistress'")
(880, "'MadMuesli'")

#

thanks a lot everyone

#

(:

lofty flume
#

Note that the natural order of "adf" and "aJf" is "aJf","adf" because of the ordinal values of the characters (uppercase has lower values than lowercase).

proud girder
lofty flume
#

That was you spec, that for the same price the usernames should be sorted.

#

If you never have 2 usernames which differ only in their case (eg "fred" and "Fred") this won't matter.

#

Unless "Fred","alf" is the wrong order.

proud girder
#

the sorting of the usernames really does not matter for me here

lofty flume
#

Aha! And there you were earlier saying that it did!

Anyway, if it doesn't matter, you're good.

proud girder
#

alr thanks again for solving the problem everyone, bye for now

#

!solved

oak jewelBOT
#
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.