# Python Tips - Convert two lists into a dictionary  Aujourd'hui nous allons voir quelque chose d'assez simple mais qui peut s'avérer très utile. Quand on programme en Python, on aime bien trouver des moyens de raccourcir le code quand c'est possible. A cet effet, Python intègre de très nombreuses fonction et méthodes de classes très utiles. --- > Mais alors qu'allons nous apprendre ? Nous allons apprendre à construire ce dictionnaire : ```Json { 'apples': 5, 'beans': 8, 'cherry': 7 } ``` À partir des listes suivantes: ```Python quantity = [5, 8, 7] item_name = ["apples", "beans", "cherry"] ``` > AH, facile ça ! il suffit de faire une petite boucle `for ... in ...:` Et vous n'auriez pas faux.. Ça donne ceci ```Python # METHOD 1 result = {} for i in range(0, min(len(quantity), len(quantity))): key = item_name[i] value = quantity[i] result[key] = value ``` Le résultat: ```Python >>>print(result) {'apples': 5, 'beans': 8, 'cherry': 7} ``` > Génial, ça marche ! Mais c'est un peu long. À ça oui c'est un peu long, c'est même plutôt long... --- Voici une méthode plus rapide: ```Python result2 = dict(zip(item_name, quantity)) ``` Le résultat: ```Python >>>print(result2) {'apples': 5, 'beans': 8, 'cherry': 7} ``` HA ! une ligne ! trop facile! > Trop bien gros malin, comment ça marche cette ligne magique ### *Décortiquons ça ensemble.* La fonction `zip()` prend en paramètre un nombre n d'itérateurs (des listes ici) et renvoie une série de tuples accolant les valeurs des listes. Un exemple vaut milles mots, regardez: ```Python >>>print(list(zip(chiffres, mots))) [('apples', 5), ('beans', 8), ('cherry', 7)] ``` Quand au `dict()` c'est le constructeur de la classe dictionnaire. Quand il prend une liste de tuple en arguments, il prend construit le dictionaire en mettant la première valeur du tuple en clé et la seconde en value.  Vous pourrez retrouver une bonne partie des articles de ce compte [sur ce repo](https://github.com/slashformotion/hive-base). Pour accéder directement la section que vous regardez actuellement [cliquer ici](https://github.com/slashformotion/hive-base/tree/master/content/series/PythonTips/PythonTips_n3-ConvertTwoListsIntoADictionary). --- Vous êtes arrivé à la fin, bravo! Si vous avez des questions, n'oubliez pas de les poser. A la prochaine !
author | slashformotion |
---|---|
permlink | python-tips-3-convert-two-lists-into-a-dictionary |
category | hive-163521 |
json_metadata | {"app":"peakd/2020.07.1","format":"markdown","tags":["python","dict","laruche","fr","stem"],"users":["thepeakstudio"],"links":["/@thepeakstudio","https://github.com/slashformotion/hive-base","https://github.com/slashformotion/hive-base/tree/master/content/series/PythonTips/PythonTips_n3-ConvertTwoListsIntoADictionary"],"image":["https://files.peakd.com/file/peakd-hive/slashformotion/hOc41Xc3-image.png","https://images.hive.blog/DQmSkA2zZKCkkqxJKiG1srWbDzi4V74KMnRmg7LfbQDka6z/dividers-06.png"]} |
created | 2020-07-03 10:45:21 |
last_update | 2020-07-03 10:45:21 |
depth | 0 |
children | 5 |
last_payout | 2020-07-10 10:45:21 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.375 HBD |
curator_payout_value | 0.370 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 2,576 |
author_reputation | 2,410,764,686,560 |
root_title | "Python Tips 3 - Convert two lists into a dictionary" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 0 |
post_id | 98,315,782 |
net_rshares | 2,758,270,974,943 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
arcange | 0 | 95,546,970,659 | 3% | ||
raphaelle | 0 | 2,600,892,652 | 3% | ||
ixindamix | 0 | 189,538,953,892 | 100% | ||
chasmic-cosm | 0 | 17,821,545,811 | 100% | ||
lugaxker | 0 | 700,477,481 | 14.85% | ||
mys | 0 | 34,210,149,731 | 10% | ||
dkmathstats | 0 | 45,192,056,581 | 100% | ||
techken | 0 | 4,544,494,202 | 7.5% | ||
whd | 0 | 12,792,605,863 | 10% | ||
fancybrothers | 0 | 3,387,962,789 | 5% | ||
rafalski | 0 | 1,231,793,167 | 10% | ||
wargof | 0 | 1,380,725,115 | 65% | ||
bambukah | 0 | 1,189,762,154 | 20% | ||
tykee | 0 | 2,986,511,243 | 50% | ||
francosteemvotes | 0 | 6,952,853,871 | 20% | ||
evildido | 0 | 1,152,708,590 | 10% | ||
fbslo | 0 | 3,805,355,106 | 5% | ||
aidefr | 0 | 3,890,407,137 | 15% | ||
robotics101 | 0 | 5,551,962,290 | 20% | ||
lpv | 0 | 1,277,388,020 | 3.75% | ||
iptrucs | 0 | 65,716,041,722 | 40% | ||
kamchore | 0 | 139,829,723,387 | 100% | ||
as31 | 0 | 6,941,017,075 | 35% | ||
duke77 | 0 | 3,191,101,904 | 25% | ||
dragibusss | 0 | 1,514,894,069 | 50% | ||
voltagrou | 0 | 1,336,081,604 | 10% | ||
gribouille | 0 | 619,902,586 | 15% | ||
orlandumike | 0 | 7,832,670,278 | 25% | ||
theophile.roos | 0 | 2,371,044,832 | 100% | ||
beleg | 0 | 4,540,707,980 | 10% | ||
imcore | 0 | 1,024,526,772 | 10% | ||
solominer | 0 | 129,303,468,163 | 4% | ||
enforcer48 | 0 | 111,370,301,168 | 15% | ||
revueh | 0 | 714,217,557 | 20% | ||
zuerich | 0 | 783,695,759,742 | 20% | ||
anttn | 0 | 61,482,344,842 | 30% | ||
julian2013 | 0 | 4,717,941,790 | 4.5% | ||
gorbisan | 0 | 4,861,979,577 | 2.5% | ||
pboulet | 0 | 7,922,394,428 | 20% | ||
oakshieldholding | 0 | 5,959,114 | 35% | ||
laissez-faire | 0 | 51,947,186 | 100% | ||
dubignyp | 0 | 1,777,771,976 | 50% | ||
solarphasing | 0 | 1,482,044,556 | 10% | ||
limka | 0 | 50,889,704 | 100% | ||
abh12345.stem | 0 | 230,525,510 | 35% | ||
oniemaniego | 0 | 331,010,033 | 5% | ||
yggdrasil.laguna | 0 | 140,425,195 | 35% | ||
davidlionfish | 0 | 10,348,018,284 | 25% | ||
fengchao | 0 | 3,476,241,316 | 3% | ||
laruche | 0 | 892,208,703,706 | 20% | ||
ykretz | 0 | 720,461,884 | 16% | ||
gitplait | 0 | 66,473,958,759 | 100% | ||
radio-quebec | 0 | 757,329,297 | 50% | ||
hive-143869 | 0 | 4,746,460,833 | 20% | ||
comprendre.hive | 0 | 727,531,760 | 20% |
@slashformotion, great tutorial but it could have been best if you had an English translation along side this post. It had to make use of Google translate to read your blog. Anyway, thanks for sharing 👍 <sub> **Your post has been submitted to be manually curated by @gitplait community account because this is the kind of publications we like to see in our community.** </sub> Join our [Community on Hive](https://hive.blog/trending/hive-103590) and Chat with us on [Discord](https://discord.gg/CWCj3rw). [[Gitplait-Team]](https://gitplait.tech/)
author | gitplait-mod2 |
---|---|
permlink | re-slashformotion-qcwhgn |
category | hive-163521 |
json_metadata | {"tags":["hive-163521"],"app":"peakd/2020.07.1"} |
created | 2020-07-03 15:53:12 |
last_update | 2020-07-03 15:53:12 |
depth | 1 |
children | 4 |
last_payout | 2020-07-10 15:53:12 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 550 |
author_reputation | 28,898,670,427 |
root_title | "Python Tips 3 - Convert two lists into a dictionary" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 98,320,582 |
net_rshares | 0 |
Thank you for the advice! I will consider that, but I'm not very good in English so ...
author | slashformotion |
---|---|
permlink | re-gitplait-mod2-qcwt4k |
category | hive-163521 |
json_metadata | {"tags":["hive-163521"],"app":"peakd/2020.07.1"} |
created | 2020-07-03 20:05:09 |
last_update | 2020-07-03 20:05:09 |
depth | 2 |
children | 3 |
last_payout | 2020-07-10 20:05:09 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 87 |
author_reputation | 2,410,764,686,560 |
root_title | "Python Tips 3 - Convert two lists into a dictionary" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 98,324,383 |
net_rshares | 0 |
Oh yeah, I totally understand. You can also make use of a translator.
author | gitplait-mod2 |
---|---|
permlink | re-slashformotion-qcwtq8 |
category | hive-163521 |
json_metadata | {"tags":["hive-163521"],"app":"peakd/2020.07.1"} |
created | 2020-07-03 20:19:06 |
last_update | 2020-07-03 20:19:06 |
depth | 3 |
children | 2 |
last_payout | 2020-07-10 20:19:06 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 69 |
author_reputation | 28,898,670,427 |
root_title | "Python Tips 3 - Convert two lists into a dictionary" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 98,324,627 |
net_rshares | 0 |