Eternal and unique value

Asked

Viewed 65 times

-3

That’s not a problem, it’s a question, so I hope you can help me.

I’m a beginner in python, and I’m learning about lists, and the teacher talks about unique, timeless elements.

I wanted to know the difference between them. I did a test, but it was on cmd.

lista1 = list(range(11))

lista1.append(14)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 14]

lista1.append([14])

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 14, [14]]


lista1.extend(55)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>

TypeError: 'int' object is not iterable

lista1.extend([55])

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 14, [15], 13]

Below is the image.

Teste no cmd

3 answers

0

As also detailed in this other reply, extend() expects as a parameter an iterable object (for example another list) and not a primitive (as an int).

Note that when you use the append method you have the option to add eternal objects or not to your list. For example: lista1.append([15]) is adding a list with a single element (the 15) to the end of list1 and not element 15.

  • Thanks man, you helped me a lot, I was looking half an hour for the meaning of this. And now I know the difference between 'extend()' and 'append()'. Thanks.

  • @Pthedark could approve that answer or some other?

0

"I wanted to know the difference between them."

Unique element: value elements, which can only have one value, (int, float, char, byte, double, decimal, etc...), are the primitive types.

Eternal element: is usually a data structure, which can contain many values, and in the case of python can count values of different data types (unique types). Iterables are usually of the Reference type (it references an address in memory), examples: lists/arrays, tuples and I think also strings, but the strings (which are a char sequence) you cannot change the value of the positions, just go through. Roughly speaking, eternal element is all that you can walk through with index notation.

-1

An iterable is a type that can be traversed. A list has several elements and you can traverse them by reading their values. (Underneath the covers, these types have the method iter implemented).

In the case of the integer, you have only one value, there is no way you can go through it by listing the values.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.