# Questions on lists, the range function & concatenation

• (2 Pages)
• 1
• 2

## 21 Replies - 2060 Views - Last Post: 25 January 2018 - 07:38 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=408915&amp;s=9986b9272187222f2d8697a0ed367220&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 2
• Posts: 27
• Joined: 22-January 18

# Questions on lists, the range function & concatenation

Posted 24 January 2018 - 08:32 AM

Hi everyone.

2nd time poster so getting use to everything.

Here is what I got going on:
I am trying to create a list that looks like this:['a', 'b', 'c', ..., 'z'} so that I can then create a dictonary out of it.

I found a way to create that and the numbers 1-26

```mport string
mylist = string.ascii_lowercase
a = "'"
b = "'"
c = ", "
for i in mylist:
letters1 = a + i + b + c
#print (letters1)

#Create range of numbers for merging
numbers = range(1,27)

```

But I am running into issues with the rest of the code I have which looks like this

```#Create range of numbers for merging
numbers = range(1,27)
d = ":"
for n in numbers:
numbers1 = str(n) + d
print (numbers1)

```

I get an error when running the code and I can't seem to figure out how to get the numbers assigned to a list. If I just print numbers it says numbers = range (1,27) almost like it is treating it like a string instead of a function.

If I loop through I get an error about mistmatched types. If I loop through and try to convert those to a string (what is above) it also doesnt work.

If possible please explain this like you are talking to a 4 year old.

Thank you!

Is This A Good Question/Topic? 0

## Replies To: Questions on lists, the range function & concatenation

### #2 DK3250

• Pythonian

Reputation: 402
• Posts: 1,297
• Joined: 27-December 13

## Re: Questions on lists, the range function & concatenation

Posted 24 January 2018 - 09:12 AM

I need some clarification here:
Are you trying to make string that looks like a dict, or do you want to actually make a dict ?

Reputation: 2
• Posts: 27
• Joined: 22-January 18

## Re: Questions on lists, the range function & concatenation

Posted 24 January 2018 - 09:21 AM

Here is the full challenge. I am trying to break it into little bits and work on each one.

#['a', 'b', 'c', ..., 'z'}
# And create a function that turns it into this dictionary:
#{1: 'a', 2: 'b', 3: 'c', ..., 26: 'z'}

#'abc...z'
#And create a function that turns it into this dictionary:
#{1: 'a', 2: 'b', 3: 'c', ..., 26: 'z'}

# 3) Take one of the above functions and include only the odd numbers / letters:
# {1: 'a', 3: 'c', ..., 25: 'y'}

Not asking anyone to write the code because that prevents me from learning but more along of the lines of what am I doing wrong because not only am I new to Python I am new to programming. I was ok at Perl about 17 years ago but for all intensive purposes we could say I am starting from scratch.

I already figured out things that are wrong. I am not appending to a list in a code thus the list is constantly being overwritten. The print statements are in the wrong place so testing the wrong things.

So I have an idea of where to go or at least I am back out of the mud. The last part is a bit challenging and havent thought about how to figure that out yet.

This post has been edited by ndc85430: 24 January 2018 - 09:48 AM
Reason for edit:: Removed quote of previous post.

### #4 DK3250

• Pythonian

Reputation: 402
• Posts: 1,297
• Joined: 27-December 13

## Re: Questions on lists, the range function & concatenation

Posted 24 January 2018 - 09:34 AM

ok, a few hints.
An easy way to make the list:
```>>> letter_list = list("abcdefghijklmnopqrstuvwxyz")
>>> letter_list
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
```

From this list you want to extract both the index value and the letter - this is easily obtained by Pythons enumerate function.
'enumerate' is often not considered beginners stuff, but it is actually quite simple:
```>>> for i, val in enumerate(letter_list):
print(i, val)

0 a
1 b
2 c
3 d
4 e
5 f
6 g
7 h
8 i
9 j
10 k
11 l
12 m
13 n
14 o
15 p
16 q
17 r
18 s
19 t
20 u
21 v
22 w
23 x
24 y
25 z
```

I hope this will be of inspiration.

Reputation: 2
• Posts: 27
• Joined: 22-January 18

## Re: Questions on lists, the range function & concatenation

Posted 24 January 2018 - 09:38 AM

Super helpful. Right now I am working on making the lists programmatically. Figured I might as well make the challenge harder so I can maximize my learnings. I will look into enumerate for the 2nd part.

Thanks!

This post has been edited by ndc85430: 24 January 2018 - 09:48 AM
Reason for edit:: Removed quote of previous post.

### #6 jon.kiparsky

• Beginner

Reputation: 11040
• Posts: 18,852
• Joined: 19-March 11

## Re: Questions on lists, the range function & concatenation

Posted 24 January 2018 - 09:39 AM

Quote

#['a', 'b', 'c', ..., 'z'}
# And create a function that turns it into this dictionary:
#{1: 'a', 2: 'b', 3: 'c', ..., 26: 'z'}

So to start with, the question here is borked - the closing curly brace should be a square bracket, like so:
```#['a', 'b', 'c', ..., 'z']

```

Now except for the elipsis, that's a list, in two senses: it's both the printed representation of a list, and it's also an expression that evaluates to a list. It's not actually the list, in some deep philosophical sense, but if you get rid of the three dots it's a complete specification of the list which is sufficient to instantiate the list in some python interpreter, and that's good enough for me.

So if we drop into the interpreter we can do something like

```>>> l = ['a', 'b', 'c', ..., 'z']
File "<stdin>", line 1
l = ['a', 'b', 'c', ..., 'z']
^
SyntaxError: invalid syntax
>>> l = ['a', 'b', 'c', 'z']
>>> l
['a', 'b', 'c', 'z']
>>> l[0]
'a'
>>> l[-1]
'z'
>>>

```

See, it's a list.

Now, we want to turn that into a dictionary which maps the index of each entry in the list to that entry's value.

You're going to want to use the initial list, which you can either type in or get from the string library:

Quote

>>> import string
>>> dir(string)
['Formatter', 'Template', '_TemplateMetaclass', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_float', '_idmap', '_idmapL', '_int', '_long', '_multimap', '_re', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'atof', 'atof_error', 'atoi', 'atoi_error', 'atol', 'atol_error', 'capitalize', 'capwords', 'center', 'count', 'digits', 'expandtabs', 'find', 'hexdigits', 'index', 'index_error', 'join', 'joinfields', 'letters', 'ljust', 'lower', 'lowercase', 'lstrip', 'maketrans', 'octdigits', 'printable', 'punctuation', 'replace', 'rfind', 'rindex', 'rjust', 'rsplit', 'rstrip', 'split', 'splitfields', 'strip', 'swapcase', 'translate', 'upper', 'uppercase', 'whitespace', 'zfill']
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> list(_)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
>>> letters = list(string.ascii_lowercase)

Then, you want to write a function that iterates over the dictionary and maps each index to the value at that index. This is the naive way to do it, we can look at some slicker ways to manage it once you've done it the naive way.

Reputation: 2
• Posts: 27
• Joined: 22-January 18

## Re: Questions on lists, the range function & concatenation

Posted 24 January 2018 - 09:45 AM

A friend sent it to me so that is just a typo (the bracket). I think the ... was to let me know all the letters. He could have wrote a-z but I think he gave me the first few letters to show the formatting.

Right now I am working on getting the letters into a list & trying to concatenate them with the 2nd list which is a list of numbers.

I think I need some kind of append command because I am constantly writing over the list. I am working on that now.

Playing with this:
```import string
mylist = string.ascii_lowercase
a = "'"
b = "'"
c = ", "
letters1 = ""
for i in mylist:
letters1.append ( a + i + b + c );
print (letters1)

```

Getting an error:

File "C:/Users/c0unt3rpl4y/PycharmProjects/House/Test 2.py", line 38, in <module>
letters1.append ( a + i + b + c );
AttributeError: 'str' object has no attribute 'append'

Process finished with exit code 1

But I think I have to figure out how to append the values for both strings & the numbers into a list before i move further.

This post has been edited by ndc85430: 24 January 2018 - 09:47 AM
Reason for edit:: Removed quote of previous post.

### #8 ndc85430

• I think you'll find it's "Dr"

Reputation: 798
• Posts: 3,260
• Joined: 13-June 14

## Re: Questions on lists, the range function & concatenation

Posted 24 January 2018 - 09:49 AM

Reputation: 2
• Posts: 27
• Joined: 22-January 18

## Re: Questions on lists, the range function & concatenation

Posted 24 January 2018 - 09:50 AM

Sorry. Thanks for that.

### #10 ndc85430

• I think you'll find it's "Dr"

Reputation: 798
• Posts: 3,260
• Joined: 13-June 14

## Re: Questions on lists, the range function & concatenation

Posted 24 January 2018 - 09:50 AM

The error is pretty clear: letters1 is a string and strings don't have an append method. You can use + to concatenate strings, but since strings are immutable be aware that that will produce a new string (so you'll have to reassign it to the original variable).

### #11 jon.kiparsky

• Beginner

Reputation: 11040
• Posts: 18,852
• Joined: 19-March 11

## Re: Questions on lists, the range function & concatenation

Posted 24 January 2018 - 09:53 AM

fad3r, on 24 January 2018 - 11:45 AM, said:

```import string
mylist = string.ascii_lowercase
a = "'"
b = "'"
c = ", "
letters1 = ""
for i in mylist:
letters1.append ( a + i + b + c );
print (letters1)

```

So here you're trying to put together a string that looks like a list. You want a list, not a string.

string.ascii_lowercase gives you a string with all of the lowercase letters of the Roman alphabet. Applying the list function to any string gives you the characters of that string as a list. So if you just want the list ['a','b','c',...'z'], then the example I gave you does exactly that.
```letters = list(string.ascii_lowercase)

```

Reputation: 2
• Posts: 27
• Joined: 22-January 18

## Re: Questions on lists, the range function & concatenation

Posted 24 January 2018 - 09:53 AM

Oh. I am still getting use to this. I wouldnt have figured that out in a like million years. Ok let me work on that and see what I come up with.

Thank you for all the help!

Reputation: 2
• Posts: 27
• Joined: 22-January 18

## Re: Questions on lists, the range function & concatenation

Posted 24 January 2018 - 10:53 AM

Ok, with your guys help I have managed to solve creating the list for a-z and then creating the string for a-z.

I am now trying to create the range of numbers.

Here is what I have tired
```#Create list of numbers
numbers = range(1,27)
list (numbers)
print (numbers)

#Create list of numbers
numbers = list((range(1,27))
print (numbers)

```

Both return range(1, 27) as if numbers were a string and range was functioning that way instead of as a function.

I also thought it might have to do with the range bringing back a string instead of an integer so I tried the int function also

```#Create list of numbers
numbers = int((range(1,27))
print (numbers)

```

This just flat out produces an error. About print being wrong

I then tried this

```#Create list of numbers
numbers = range(1,27)
int (numbers)
print (numbers)

```

And this gave an interesting error with: int (numbers)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'range'

Which leads me to believe the range function returns numbers already so not sure why when I try to print them or put them in a list I am getting errors.

### #14 ndc85430

• I think you'll find it's "Dr"

Reputation: 798
• Posts: 3,260
• Joined: 13-June 14

## Re: Questions on lists, the range function & concatenation

Posted 24 January 2018 - 11:00 AM

No. The function returns and object in Python 3, that's basically something you can iterate over. int expects à strings or something else that can be converted to a number, not a sequence of things to be converted.

You'll probably want to read up on how generators and iterators work.

### #15 jon.kiparsky

• Beginner

Reputation: 11040
• Posts: 18,852
• Joined: 19-March 11

## Re: Questions on lists, the range function & concatenation

Posted 24 January 2018 - 11:06 AM

Belay the part about what the range actually is, for now at least. It's an interesting question and you'll enjoy looking into it, but it's not the important thing for solving this problem. What you care about here is that a range is something that you can iterate over:
```for i in range (1, 11):
print(i)  # prints the numbers from 1 to 10 inclusive, each on a line

```

Knowing that, you should be able to create a dictionary, and then iterate over the numbers from 1 to 27, and add an entry to the dictionary for each of them

If creating the dictionary is hard, start there: for each integer from 1 to 10, map that number to itself. End result will be something like {1:1, 2:2, 3:3, ... 10:10}