Nesa
Nesa

Reputation: 2965

Python list and range

Could you tell me how I can write a working code if I don't know how many items the list contains (without using the len function, so I want a code that simply ignores the rest of the range if it is longer than the list). e.g. this code only works with the given range and not range (0,10)

   gfgf=[[1],[2]]
        for i in range (0,1):
            if 1 in gfgf[i] :
                print (gfgf)

Also could you tell me what we call the components of a list ([1] and [2] of gfgf in the example). We call the components of a dictionary 'items '.

Upvotes: 0

Views: 91

Answers (2)

ricpacca
ricpacca

Reputation: 828

I would write that function this way:

gfgf=[[1],[2]]
for i in gfgf:
    if 1 in i:
        print(gfgf)

Hope this helps!

Upvotes: 0

Eugene K
Eugene K

Reputation: 3457

gfgf=[[1],[2]]
for x in gfgf:
    if 1 in x:
        print(x)

You can simply just do for x in mylist

Upvotes: 2

Related Questions