Python Forum
simple list check with loops
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
simple list check with loops
#11
Both list and dict are data structures or containers.
You can access list element by index. List by design are ordered. You can have same element multiple times in the list.
Dict is not ordered by design. you have key:value pairs. You access the value using the key. So keys are unique (you cannot have the same key multiple times in the dict).
That's very very basic explanation. You should read the docs and/or some tutorial, e.g. dict and lists

from practical point of view, here are two cases that show the benefits of using dict vs. list
I. what is the password for given user. e.g. what is the password for ryan? with list of lists as in your current design you should iterate over list elements (the users), check if the name is ryan (i. .e. usr[0]) if usr[0] is ryan, return usr[1] (i.e. the password). You should loop over elements every time you want to get some user password. With dict you just get the value (the password) associated key ryan. See the performance difference?
users_list = [['josh' , 'mi'], ['ryan' , 'th'], ['loki' , 'ch']]
for user in users_list:
    if user[0] == 'ryan':
        print (user[1])
        break # this will exit the loop once name ryan is found
vs.

users_dict = {'josh':'mi', 'ryan':'th', 'loki':'ch'}
print users_dict['ryan']
II. Keys are unique and actually you want the same for the usernames. Already that should hint in the direction that maybe the dict is better choice for the task
following is correct code

users_list = [['josh' , 'mi'], ['josh' , 'ns'], ['ryan' , 'th'], ['loki' , 'ch']]
while following is not
users_dict = {'josh':'mi', 'josh':'ns', 'ryan':'th', 'loki':'ch'}
Of course there is a lot more to be said about lists vs. dict and their usage...
Reply


Messages In This Thread
simple list check with loops - by Low_Ki_ - Jan-05-2017, 07:09 AM
RE: simple list check with loops - by Larz60+ - Jan-05-2017, 10:44 AM
RE: simple list check with loops - by stranac - Jan-05-2017, 10:50 AM
RE: simple list check with loops - by buran - Jan-05-2017, 06:53 PM
RE: simple list check with loops - by Low_Ki_ - Jan-06-2017, 04:48 AM
RE: simple list check with loops - by Kebap - Jan-06-2017, 12:46 PM
RE: simple list check with loops - by Low_Ki_ - Jan-06-2017, 04:32 PM
RE: simple list check with loops - by Low_Ki_ - Jan-06-2017, 05:47 PM
RE: simple list check with loops - by buran - Jan-06-2017, 06:47 PM
RE: simple list check with loops - by Low_Ki_ - Jan-06-2017, 07:10 PM
RE: simple list check with loops - by buran - Jan-06-2017, 08:31 PM
RE: simple list check with loops - by Low_Ki_ - Jan-08-2017, 04:13 AM
RE: simple list check with loops - by Low_Ki_ - Jan-08-2017, 05:33 AM
RE: simple list check with loops - by Larz60+ - Jan-08-2017, 05:53 AM
RE: simple list check with loops - by Low_Ki_ - Jan-08-2017, 07:19 AM
RE: simple list check with loops - by buran - Jan-08-2017, 07:35 AM
RE: simple list check with loops - by Low_Ki_ - Jan-08-2017, 07:45 AM
RE: simple list check with loops - by buran - Jan-08-2017, 08:01 AM
RE: simple list check with loops - by Larz60+ - Jan-08-2017, 12:21 PM
RE: simple list check with loops - by Low_Ki_ - Jan-09-2017, 01:52 AM
RE: simple list check with loops - by snippsat - Jan-08-2017, 01:42 PM
RE: simple list check with loops - by Larz60+ - Jan-09-2017, 02:47 AM
RE: simple list check with loops - by Low_Ki_ - Jan-09-2017, 03:18 AM
RE: simple list check with loops - by Larz60+ - Jan-09-2017, 03:58 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 586 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  [solved] list content check paul18fr 6 879 Jan-04-2024, 11:32 AM
Last Post: deanhystad
  for loops break when I call the list I'm looping through Radical 4 1,006 Sep-18-2023, 07:52 AM
Last Post: buran
  How to solve this simple problem? Check if cvs first element is the same in each row? thesquid 2 1,328 Jun-14-2022, 08:35 PM
Last Post: thesquid
  check if element is in a list in a dictionary value ambrozote 4 2,120 May-11-2022, 06:05 PM
Last Post: deanhystad
  How to check if a list is in another list finndude 4 1,958 Jan-17-2022, 05:04 PM
Last Post: bowlofred
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,602 Aug-12-2021, 04:25 PM
Last Post: palladium
  Using recursion instead of for loops / list comprehension Drone4four 4 3,269 Oct-10-2020, 05:53 AM
Last Post: ndc85430
  how to check if string contains ALL words from the list? zarize 6 7,491 Jul-22-2020, 07:04 PM
Last Post: zarize
  Creating a List with many variables in a simple way donnertrud 1 2,105 Jan-11-2020, 03:00 PM
Last Post: Clunk_Head

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020