Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

a_list: List[T] = []


sort by: page size:

Can't you just:

    def __iter__(self):
        return iter(self.a_list)

What do a-list/p-list have to do with sharing structure?

If you want shared structure of lists in Python, use `itertools.chain`.

If you want key-value mappings, use a dict. Order is preserved by default nowadays, and conversion to/from an iterable of 2-tuples is trivial.


In python, you can simply substitute `A` with an iterable or generator object, which can be a of unknown length.

Lists in Python are always references.

There’s a separate type called tuple specifically designed to represent lists-as-values.

If you do [()]*4 then you get exactly what you want.


Yeah in Python you can do, say, a = b = []

But then the two lists are the same, and appending to one appends to the other. Not particularly useful.


Do you mean ``` def add_item_to_list(item, list_to_add=[]): list_to_add.append(item) return list_to_add

  a = add_item_to_list(1)
  a.append(3)
  b = add_item_to_list(2)
```

this list is awesome. made my morning.

anyone know why this works in python

  lst=[]
  lst+=2, # note , in the end
  print(lst) #[2]

A wild Python suddenly appears:

  third_list = [*first_list, *second_list]
You don't even need a "list context" for this!

Python is not unique in this regard. Consider a functional approach, where you leave the original list intact and construct a changed one.

He's using the incorrect datatype for ap_list. This is apprently the collection of AP's already seen, and used with this pattern:

  ap_list = []
  if AP not in ap_list:
      ap_list.append(AP)
      print AP
It works, but does a linear scan every time. It's better to use a set() in such a case:

  ap_seen = set()
  if AP not in ap_seen:
      ap_seen.add(AP)
      print AP
For looking up things, a set (essentially a dict without values) is much quicker than a list. Is this premature optimisation? Perhaps, but using a list for this purpose is also a code smell.

The `list(range(n))` is for Python 3 compatibility :)

The [:] you mentioned is indeed redundant, I removed it. Thanks.


I thought it seemed odd that Python supported the AND operator on lists, but it doesn't.

You do need to convert to a set first, e.g.

  [1,2,3] & [1,2]
gives a TypeError, while

  set([1,2,3]) & set([1,2])
gives set([1,2]) as expected.

Great PEP :-)

It also mentions further down, that for lists er have both

  a + b
and

  [*a, *b]
As well as other ones I hadn't even thought about like a[len(a):] = b.

A much more interesting line of python is

b[:] = a # copy a into b, while b keeps its identity.


Python lists are backed by contiguous memory of pointers to other objects, mind.

+= on lists is an annoying one:

    def myfunc(mylist):
        mylist += [1,2,3]
is NOT the same as:

    def myfunc(mylist):
        mylist = mylist + [1,2,3]
Try this on each of those definitions:

    a = [4,5,6]
    myfunc(a)
    print a
The first one mutates 'a', the second only reassigns (and discards) a temporary `mylist`.

Put another way, += is not just syntactic sugar. With lists, it actually mutates the original list.


I think that only works if `result` is a list

I think this is great, I've been doing Python for a while and I knew many of the features but I also learned a few new ones.

I don't understand how this one to flatten lists works:

    a = [[1, 2], [3, 4], [5, 6]]
    [x for l in a for x in l]
Can somebody explain what the order of operations is here and what the variables refer to in the various stages of evaluation?

This is still going strong today in list slice notation in Python (and other languages, no doubt):

    x = [1,2,5,6]
    x[2:2] = [3,4]
    print(x)
(Slice notation can be used on any collection, not just lists, but you can't use it in assignment like that for strings because they're immutable, so you'd have to convert to a list of characters and back.)
next

Legal | privacy